Python - 程序没有正确退出循环

时间:2017-11-08 05:59:11

标签: python while-loop breakout

我只是一个初学者,但我无法弄清楚为什么我的代码没有正确退出循环。

import uuid

class Bank_Account:
    def __init__(self, name, id, balance):
        self.name = name
        self.id = id
        self.balance = balance

    def deposit(self, amount):
        self.balance = self.balance + amount

    def can_withdraw(self, amount):
        if  (amount <= self.balance):
            return True
        else:
            return False

    def withdraw(self, amount):
        self.balance = self.balance - amount

    def printAll(self,outfile):
        print(self.name, "|", self.id, "|", self.balance, file = outfile)

    def balance(self):
        return self.balance

def main():
    menu = eval(input("Main Menu. Type 1 for new customer, 2 for existing, 0 
        to exit. "))

    while (menu != 0):
        if (menu == 1):
            name = input("Enter account holder: ")
            balance = eval(input("Enter beginning balance: "))
            id = uuid.uuid4()
            account = Bank_Account(name,id,balance)

            first,last = name.split()
            fileName = first + last
            outfile = open(fileName, "w" )
            account.printAll(outfile)

            print("********Account created********")
            print("Account number: ", id)
            print("Account name: ", name)
            print("New balance: ", balance)
            print("*****************************")
            outfile.close()
            menu = eval(input("Main Menu. Type 1 for new customer, 2 for 
                         existing, 0 to exit. "))

          elif (menu == 2):
            name = input("Enter account holder: ")

            first,last = name.split()
            fileName = first + last
            outfile = open(fileName, "r")

            lines = outfile.readlines()
            for line in lines:
                info = line.split("|")
            name = info[0]
            id = info[1]
            balance = int(info[2])
            account = Bank_Account(name,id,balance)

            print("Summary:\n", name, "Account Number: ", id, "Balance: ", 
                  balance)

            outfile.close()

            menu2 = eval(input("Type 1 to deposit, 2 to withdraw, 3 for 
                          balance, 0 to return to main menu: "))

            while (menu2 != 0):
                if (menu2 == 1):
                    outfile = open(fileName, "w")
                    amount = eval(input("How much would you like to deposit? 
                                          "))
                    account.deposit(amount)
                    account.printAll(outfile)
                    print("Account number: ", id)
                    print("Account name: ", name)
                    print("New balance: ", account.balance)
                    outfile.close()
                    menu2 = eval(input("Type 1 to deposit, 2 to withdraw, 3 
                             for balance, 0 to return to main menu: "))
                elif (menu2 == 2):
                    outfile = open(fileName, "w")
                    amount = eval(input("How much would you like to 
                                         withdraw? "))
                    withdrawal = account.can_withdraw(amount)
                    if (withdrawal == True):
                        account.withdraw(amount)
                        account.printAll(outfile)
                        print("Account number: ", id)
                        print("Account name: ", name)
                        print("New balance: ", account.balance)
                        menu2 = eval(input("Type 1 to deposit, 2 to 
                      withdraw, 3 for balance, 0 to return to main menu: "))
                    else:
                        print("Error, attempted to withdraw more than total 
                        balance, try again.")
                    outfile.close()
                elif (menu2 == 3):
                    print("Your account balance: ", account.balance)
                    menu2 = eval(input("Type 1 to deposit, 2 to withdraw, 3 
                              for balance, 0 to return to main menu: "))



    print("Have a nice day!")



main()

我的问题是,在为menu2输入0时,它再次进入 elif (menu == 2) : name = input("Enter account holder: ")。相反,它应该提示外循环的输入。为什么代码没有突破这个内循环?

3 个答案:

答案 0 :(得分:0)

你必须在while循环结束时再次询问用户他的选择。变量menu的值不会因您的情况而改变。

menu = eval(input("Main Menu. Type 1 for new customer, 2 for existing, 0 to exit. "))
while (menu != 0):
    if (menu == 1):
        .....
    elif (menu == 2):
        .....
    menu2 = eval(input("Type 1 to deposit, 2 to withdraw, 3 for balance, 0 to return to main menu: "))

    while (menu2 != 0):
        .....
    menu = eval(input("Main Menu. Type 1 for new customer, 2 for existing, 0 to exit. "))

答案 1 :(得分:0)

第一个输入行:

menu = eval(input("Main Menu. Type 1 for new customer, 2 for existing, 0 
    to exit. ")) 

您甚至可以使用:

  menu = int(input("Main Menu. Type 1 for new customer, 2 for existing, 0 
    to exit. ")) 

这应该在while循环中,如果你希望它打破外循环

建议:当你在罗马时,应该有更多的pythonic方法来实现同样的目标

答案 2 :(得分:0)

在主要功能结束时添加此行: -

menu = int(input("Main Menu. Type 1 for new customer, 2 for 
                         existing, 0 to exit. "))

菜单2为0后,您没有移动到主菜单,这就是为什么它没有正确打破。