我只是一个初学者,但我不明白为什么我的代码没有正确退出循环。
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()
我的问题是,在为菜单2输入0时,它再次转到CCD_ 1。相反,它应该在外部循环中提示输入。为什么代码没有突破这个内部循环?
在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. "))
第一个输入行:
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循环内,如果你希望它中断到外部循环
建议:应该有更多的蟒蛇方式来实现同样的目的,当你在罗马时,就像罗马人一样
在主函数末尾添加此行:-
menu = int(input("Main Menu. Type 1 for new customer, 2 for
existing, 0 to exit. "))
在menu2为0之后,您不会移动到主菜单,这就是为什么它没有正确断开的原因。