Python basics 05 loop variable function combination case

Table of contents

1 Introduction: 

2. Case details:

-> 2.1 Case Dismantling

---> 2.1.1 Home page function: 

---> 2.1.2 Check balance:

---> 2.1.3 Saving money

---> 2.1.4 Withdraw money

---> 2.1.5 Return to the home page and try again [optional]

---> 2.1.6 Exit

3. Implementation code (python version)

4. Running result: 

-> 4.1 If you want to write in text format, you can directly copy the text

-> 4.2 Diagram (clearer)

5. Summary: 


1 Introduction: 

You can see the previous articles in the column. According to the order of numbers, the code found and adapted from the case online is handwritten by myself.

 Base directory: 

  1. Variables and Data Types: Learn about the different data types in Python and how to define variables.

  2. Operators: Learn various operators in Python such as arithmetic, comparison, logic, membership operators, etc.

  3. Control flow statements: Learn various control flow statements in Python, such as conditional statements, loop statements, etc.

  4. Functions and Modules: Learn how to define functions and how to use modules to organize your code.

  5. File operations: Understand how to read and write files.

  6. Exception Handling: Understand how to handle exceptional situations.

  7. Data Structure: Understand the commonly used data structures in Python, such as lists, tuples, dictionaries, sets, etc.

  8. Object-Oriented Programming: Learn object-oriented programming in Python, such as classes, objects, inheritance, polymorphism, and more.

  9. Regular Expressions: Learn how to use regular expressions for string matching and replacement.

  10. Use of the standard library: learn commonly used modules and functions in the Python standard library

A new case of loop variable function combination based on python 

2. Case details:

The cash register and cash withdrawal process of ATM roughly borrowed from the online case, I am too lazy to make up

-> 2.1 Case Dismantling

---> 2.1.1 Home page function: 

Please enter the function you want to operate: 1 Check balance 2 Deposit money 3 Withdraw money 4 Return to homepage (retry) 5 Exit

---> 2.1.2 Check balance:

show the current balance

---> 2.1.3 Saving money

Ask how much to save Return to the home page

---> 2.1.4 Withdraw money

Ask how much to withdraw and return to the main page

---> 2.1.5 Return to the home page and try again [optional]

Return to the home page (meaning retry)

---> 2.1.6 Exit

(logout username first, then logout)

3. Implementation code (python version)

It’s not necessary to write it in java. It’s a very simple case.

There may be a better way of writing this case  

"""
 四个选项  查询余额  存钱 取钱 退出
 存钱 取钱 查余额都要显示当前余额
"""
money = 5000
name = None


def show_balance():
    print(f"用户姓名: {name} , 余额是: {money}元")
    main_menu()


def add_balance(add_money):
    global money
    money += add_money
    show_balance()


def sub_balance(sub_money):
    """
    取钱 注意 钱没了就不能取了
    :param sub_money: 取的钱数
    :return: None
    """
    global money
    if sub_money <= money:
        money -= sub_money
    else:
        print("操作失败, 余额不足, ", end="")
    show_balance()


def exit_active():
    global name
    name = None


def main_menu():
    global name

    if not name:
        name = input("请输入您的姓名: \n")
    print(f"当前登录的用户名字为: {name}")

    input_num = int(input("请输入您要操作的功能: 1查询余额 2存钱 3取钱 4返回主页(重试) 5退出\n"))

    if input_num == 1:
        print("1 查询余额操作------> ")
        show_balance()

    if input_num == 2:
        print("2 存钱操作------> ")
        add_balance(int(input("请输入存入金额: \n")))

    if input_num == 3:
        print("3 取钱操作------> ")
        sub_balance(int(input("请输入存入金额: \n")))

    if input_num == 4:
        print("4返回主页------> ")
        main_menu()

    if input_num == 5:
        print("5 退出------> ")
        exit_active()
        return None


# 程序入口
while True:
    if not main_menu():
        break
print(f"当前用户退出成功, 现在登录的用户是: {name}")

4. Running result: 

-> 4.1 If you want to write in text format, you can directly copy the text

Please enter your name: 
pzy
The name of the currently logged in user is: pzy
Please enter the function you want to operate: 1 Check balance 2 Deposit money 3 Withdraw money 4 Return to the home page (retry) 5 Exit
1
1 Check balance operation ---- --> 
User name: pzy , balance: 5000 yuan
The current login user name is: pzy
Please enter the function you want to operate: 1 Check the balance 2 Deposit money 3 Withdraw money 4 Return to the main page (retry) 5 Exit
2
2 Deposit Money operation ------> 
Please enter the deposit amount: 
100
User name: pzy , balance: 5100 yuan
The current login user name is: pzy
Please enter the function you want to operate: 1 Check the balance 2 Deposit 3 Withdraw Money 4 Return to the home page (retry) 5 Exit
3
3 Withdrawal operation ------> 
Please enter the deposit amount: 
50
User name: pzy , balance: 5050 yuan
The current login user name is: pzy
Please enter your Functions to be operated: 1 Check balance 2 Deposit money 3 Withdraw money 4 Return to the homepage (retry) 5 Exit
4
4 Return to the homepage ------> 
The name of the currently logged in user is: pzy
Please enter the function you want to operate: 1 Check balance 2 Deposit money 3 Withdraw money 4 Return to homepage (retry) 5 Exit
5
5 Exit ------> 
The current user has logged out successfully, the current logged in user is: None

-> 4.2 Diagram (clearer)

5. Summary: 

---> Function analysis steps:

In the above case, at the beginning, think clearly about which functions are shared 

1. First make all functions into functions

2. Analyze the global variable, what is the balance and the name of the registrant

3. The analysis subject judges that it is an infinite loop and finds the exit of the program at the same time (enter 5 to exit to exit (error reporting is also possible))

4. Observe that each function puts the common method into each individual method (deposit/withdraw money -> check balance) 

5. Is it necessary to return to the home page after checking the balance function (check the balance -> return to the home page main method)

6. Determine the login user name, if the value is not None, continue to operate, otherwise enter the user name

7. Exit is not only the program exit but also the user name needs to be cleared

8. Carry out a test, pay attention to test whether the money that exceeds the balance can be withdrawn (the operation fails, the balance is insufficient)


Guess you like

Origin blog.csdn.net/pingzhuyan/article/details/132280753