这样做之后如何让它循环回到开始?在每次交易之后,它结束并且不会返回以查看您是否可以选择其他选项。 谢谢,非常感谢。
balance=7.52
print("Hi, Welcome to the Atm.")
print("no need for pin numbers, we already know who you are")
print("please selection one of the options given beneath")
print("""
D = Deposit
W = Withdrawal
T = Transfer
B = Balance check
Q = Quick cash of 20$
E = Exit
Please select in the next line.
""")
option=input("which option would you like?:")
if option==("D"):
print("How much would you like to deposit?")
amount=(int(input("amount:")))
total=amount+balance
elif option ==("W"):
print("How much would you like to withdrawl?")
withdrawl=int(input("how much would you like to take out:?"))
if balance<withdrawl:
print("Error, insufficent funds")
print("please try again")
elif option == "T":
print("don't worry about the technicalities, we already know who you're transferring to")
transfer =int(input("How much would you like to transfer:?"))
print("you now have", balance-transfer,"dollars in your bank")
elif option=="B":
print("you currently have",balance,"dollars.")
elif option=="Q":
print("processing transaction, please await approval")
quicky=balance-20
if balance<quicky:
print("processing transaction, please await approval")
print("Error, You're broke.:(")
elif option=="E":
print("Thanks for checking with the Atm")
print("press the enter key to exit")
答案 0 :(得分:0)
好像你问的是一个sentinel value的循环。
在您打印菜单之前的某处,设置一个标记值:
keep_going = True
然后,最好在下一行(打印你想要在循环时看到的第一件事)之前,开始循环。
while keep_going: # loop until keep_going == False
如上所述,这是一个无限循环。 while
语句下面的缩进块中的所有内容都将按顺序重复。这显然不是我们想要的 - 我们必须有一些方法才能离开,这样我们就可以将我们的电脑用于其他事情了!这就是我们的哨兵进来的地方。
构建新菜单选项以允许用户退出。假设您将其键入&#34; Q&#34;,然后用户选择它。然后,在那个分支中:
elif option == 'Q':
keep_going = False
由于那个分支中的所有内容都存在,我们&#34;脱落&#34;循环的底部,然后返回while
语句,该语句现在无法检查。循环终止了!
顺便说一下,你应该考虑阅读The Python Style Guide。它非常易于阅读,让您了解如何使您的代码也易于阅读。大多数Python程序员都遵守它并期望其他人也这样做,所以你也应该这样做!如果您需要帮助来学习它,或者不确定您是否正确行事,则可以tools到check your code来帮助您保持清洁和无错误。
快乐的节目!