首页 > 解决方案 > 如何让代码重新运行已经采用的旧路径?

问题描述

我正在尝试创建一个 ATM,要求输入用户名登录,然后每个用户都有三个单独的帐户可供选择。在这些帐户中的每一个中,他们都允许他们存款、取款和查看余额。我的问题是我对列表不是很好,我相信这是需要的。一旦我已经扔掉了代码,我就无法获得以新用户身份登录的代码。示例:我创建了一个用户 Bob 并登录并存入资金。然后我退出 Bob 并想创建一个新用户 Tim。当我创建蒂姆时,它不会让我记录它。每次我输入 Tim 时,它都会一直给我相同的菜单。

我相信我需要创建一个用户列表,然后为每个用户创建一个列表,我不明白该怎么做。从我的代码来看,我只是为每个账户中的资金使用设定值。这可能是为什么主登录不允许我使用其他用户的问题吗?

user_list = []
data_list = []
index = -1

user_input = 0
user_account = 0
credit_input = 0
checking_input = 0
saving_input = 0

while user_input != 3:
    print("1: Login\n2: New User\n3: Exit")
    user_input = int(input("Please pick an option: "))

    if user_input == 1:
        username = input("Login: ")
        while username not in user_list:
            username = input("No user, try again: ")
        index = user_list.index(username)
        while user_account != 4:
            print("Accounts:\n\n1: Credit\n2: Checking\n3: Savings\n4:Exit ")
            user_account = int(input("Please pick an option: "))
            if user_account == 1:
                credit_funds = 0
                while credit_input != 4:
                    print("1: Deposit")
                    print("2: Withdraw")
                    print("3: Credit Account Balance")
                    print("4: Exit") 
                    credit_input = int(input("Pick an option: "))
                    if credit_input == 1:
                        number = int(input("Deposit amount: "))
                        credit_funds += number
                        print("Deposit of $", number)
                    elif credit_input == 2:
                        number = int(input("Withdraw amount: "))
                        while number > credit_funds:
                            print("\nInsufficient Funds")
                            break
                        else:
                            credit_funds -= number
                            print("\nSuccessful Withdraw of $", number)
                    elif credit_input == 3:
                        print("Avalable balance: $", credit_funds)


            elif user_account == 2:
                checking_funds = 0
                while checking_input != 4:
                    print("1: Deposit")
                    print("2: Withdraw")
                    print("3: Checking Account Balance")
                    print("4: Exit")
                    checking_input = int(input("Pick an option: "))
                    if checking_input == 1:
                        amount = int(input("Deposit amount: "))
                        checking_funds += amount
                        print("Deposit of $", amount)
                    elif checking_input == 2:
                        amount = int(input("Withdraw amount: "))
                        while amount > checking_funds:
                            print("\nInsufficient Funds")
                            break
                        else:
                            checking_funds -= amount
                            print("\nSuccessful Withdraw of $", amount)
                    elif checking_input == 3:
                        print("Avalable balance: $", checking_funds)


            elif user_account == 3:
                saving_funds = 0
                while saving_input != 4:
                    print("1: Deposit")
                    print("2: Withdraw")
                    print("3: Saving Account Balance")
                    print("4: Exit")
                    saving_input = int(input("Pick an option: "))
                    if saving_input == 1:
                        number3 = int(input("Deposit amount: "))
                        saving_funds += number3
                        print("Deposit of $", number3)
                    elif saving_input == 2:
                        number3 = int(input("Withdraw amount: "))
                        while number3 > saving_funds:
                            print("\nInsufficient Funds")
                            break
                        else:
                            saving_funds -= number3
                            print("\nSuccessful Withdraw of $", number3)
                    elif saving_input == 3:
                        print("Avalable balance: $", saving_funds)

    elif user_input == 2:
        username = input("Please pick a username: ")
        while username in user_list:
            username = input("Pick another please: ")
        user_list.append(username)

标签: pythonpython-3.xjupyter-notebook

解决方案


当用户“注销”(登录时按 4)时,您设置user_account为 4(退出条件)。在那之后它永远不会被取消。因此,当另一个用户尝试登录时,程序会进行测试user_account != 4,但不会通过该测试,并且永远不会进入 while 循环 ( while user_account != 4)。我怀疑所有其他退出条件也会发生同样的情况。

我建议在采取适当措施后将任何输入的值重置为 0。或者,在达到退出条件时while True:显式使用和。break

例如:

while True:
    print("1: Login\n2: New User\n3: Exit")
    user_input = int(input("Please pick an option: "))

    if user_input == 1:
        print("Option 1 selected")
        # DO SOMETHING
    elif user_input == 2:
        print("Option 2 selected")
        # DO SOMETHING
    elif user_input == 3:
        break

推荐阅读