为什么我的循环没有在我设置的数字处停止?
Why is my loop not stopping at the number I set?
我正在 python 中使用数组和函数为银行应用程序编写程序。这是我的代码:
NamesArray=[]
AccountNumbersArray=[]
BalanceArray=[]
def PopulateAccounts():
for position in range(5):
name = input("Please enter a name: ")
account = input("Please enter an account number: ")
balance = input("Please enter a balance: ")
NamesArray.append(name)
AccountNumbersArray.append(account)
BalanceArray.append(balance)
def SearchAccounts():
accounttosearch = input("Please enter the account number to search: ")
for position in range(5):
if (accounttosearch==NamesArray[position]):
print("Name is: " +position)
break
if position>5:
print("The account number not found!")
print("**** MENU OPTIONS ****")
print("Type P to populate accounts")
print("Type S to search for account")
print("Type E to exit")
choice = input("Please enter your choice: ")
while (choice=="E") or (choice=="P") or (choice=="S"):
if (choice=="P"):
PopulateAccounts()
elif (choice=="S"):
SearchAccounts()
elif (choice=="E"):
print("Thank you for using the program.")
print("Bye")
当用户输入 "P" 时,它应该调用 def PopulateAccounts()
并且它确实调用了,但问题是它不会停止并且用户必须继续输入帐户名、帐户号码和账户余额。它应该在第 5 个名字之后停止。我该如何解决这个问题?
您的代码只要求用户选择一次——在循环开始之前。因为它永远不会改变,所以该循环将坚持用户的选择进行无限次迭代。
choice = input("Please enter your choice: ")
while (choice=="E") or (choice=="P") or (choice=="S"):
if (choice=="P"):
PopulateAccounts()
elif (choice=="S"):
SearchAccounts()
elif (choice=="E"):
print("Thank you for using the program.")
print("Bye")
# here at the end of this loop, you should
# get the user to enter another choice for the next
# iteration.
这是因为在PopulateAccounts()
完成后while
循环继续迭代,因为choice
仍然是P
。如果您想要求用户执行其他操作,只需再次要求他输入即可。
choice = input("Please enter your choice: ")
while (choice=="E") or (choice=="P") or (choice=="S"):
if (choice=="P"):
PopulateAccounts()
elif (choice=="S"):
SearchAccounts()
elif (choice=="E"):
print("Thank you for using the program.")
print("Bye")
choice = input("Please enter another action: ")
此外,我建议您使用无限循环来不断询问用户输入,并在用户输入时中断它 'E',这样您还可以跟踪无效输入。
while True:
choice = input("Please enter your choice: ")
if choice == "P":
PopulateAccounts()
elif choice == "S":
SearchAccounts()
elif choice == "E":
print("Thank you for using the program.")
print("Bye")
break
else:
print("Invalid action \"{}\", avaliable actions P, S, E".format(choice))
print()
你的while
循环没有计数器让它在第5个名字处停止,position
只在它所在的函数执行期间存在。另外,position
永远不会大于 4。range(5)
从 0 开始到 4 结束。
您的 for
循环没问题。问题是您的 while
循环在重复。所以在调用 PopulateAccounts()
之后,它在 运行 之后通过 for
循环 5 次正确完成,但是由于 choice
仍然等于 "P"(这还没有t 在用户第一次输入后没有改变),你仍然停留在 while
循环中,这意味着 PopulateAccounts()
将被一次又一次地调用。您可以通过在 "while" 行之后添加一个附加语句(如“print("Hey, we're at the top of the While loop!")
”)来验证这一点。
如果用户选择 "E":
,请尝试使用显式中断重写您的 while 循环
while True:
if (choice=="P"):
PopulateAccounts()
elif (choice=="S"):
SearchAccounts()
elif (choice=="E"):
print("Thank you for using the program.")
print("Bye")
quit()
choice = input("Please enter either P, S or E: ")
请注意,如果用户键入除 "P"、"S" 或 "E" 之外的其他内容,底部的这个额外输入也很方便。您可能还需要考虑将 .upper()
添加到 choice
检查中以使其不区分大小写。
我正在 python 中使用数组和函数为银行应用程序编写程序。这是我的代码:
NamesArray=[]
AccountNumbersArray=[]
BalanceArray=[]
def PopulateAccounts():
for position in range(5):
name = input("Please enter a name: ")
account = input("Please enter an account number: ")
balance = input("Please enter a balance: ")
NamesArray.append(name)
AccountNumbersArray.append(account)
BalanceArray.append(balance)
def SearchAccounts():
accounttosearch = input("Please enter the account number to search: ")
for position in range(5):
if (accounttosearch==NamesArray[position]):
print("Name is: " +position)
break
if position>5:
print("The account number not found!")
print("**** MENU OPTIONS ****")
print("Type P to populate accounts")
print("Type S to search for account")
print("Type E to exit")
choice = input("Please enter your choice: ")
while (choice=="E") or (choice=="P") or (choice=="S"):
if (choice=="P"):
PopulateAccounts()
elif (choice=="S"):
SearchAccounts()
elif (choice=="E"):
print("Thank you for using the program.")
print("Bye")
当用户输入 "P" 时,它应该调用 def PopulateAccounts()
并且它确实调用了,但问题是它不会停止并且用户必须继续输入帐户名、帐户号码和账户余额。它应该在第 5 个名字之后停止。我该如何解决这个问题?
您的代码只要求用户选择一次——在循环开始之前。因为它永远不会改变,所以该循环将坚持用户的选择进行无限次迭代。
choice = input("Please enter your choice: ")
while (choice=="E") or (choice=="P") or (choice=="S"):
if (choice=="P"):
PopulateAccounts()
elif (choice=="S"):
SearchAccounts()
elif (choice=="E"):
print("Thank you for using the program.")
print("Bye")
# here at the end of this loop, you should
# get the user to enter another choice for the next
# iteration.
这是因为在PopulateAccounts()
完成后while
循环继续迭代,因为choice
仍然是P
。如果您想要求用户执行其他操作,只需再次要求他输入即可。
choice = input("Please enter your choice: ")
while (choice=="E") or (choice=="P") or (choice=="S"):
if (choice=="P"):
PopulateAccounts()
elif (choice=="S"):
SearchAccounts()
elif (choice=="E"):
print("Thank you for using the program.")
print("Bye")
choice = input("Please enter another action: ")
此外,我建议您使用无限循环来不断询问用户输入,并在用户输入时中断它 'E',这样您还可以跟踪无效输入。
while True:
choice = input("Please enter your choice: ")
if choice == "P":
PopulateAccounts()
elif choice == "S":
SearchAccounts()
elif choice == "E":
print("Thank you for using the program.")
print("Bye")
break
else:
print("Invalid action \"{}\", avaliable actions P, S, E".format(choice))
print()
你的while
循环没有计数器让它在第5个名字处停止,position
只在它所在的函数执行期间存在。另外,position
永远不会大于 4。range(5)
从 0 开始到 4 结束。
您的 for
循环没问题。问题是您的 while
循环在重复。所以在调用 PopulateAccounts()
之后,它在 运行 之后通过 for
循环 5 次正确完成,但是由于 choice
仍然等于 "P"(这还没有t 在用户第一次输入后没有改变),你仍然停留在 while
循环中,这意味着 PopulateAccounts()
将被一次又一次地调用。您可以通过在 "while" 行之后添加一个附加语句(如“print("Hey, we're at the top of the While loop!")
”)来验证这一点。
如果用户选择 "E":
,请尝试使用显式中断重写您的 while 循环while True:
if (choice=="P"):
PopulateAccounts()
elif (choice=="S"):
SearchAccounts()
elif (choice=="E"):
print("Thank you for using the program.")
print("Bye")
quit()
choice = input("Please enter either P, S or E: ")
请注意,如果用户键入除 "P"、"S" 或 "E" 之外的其他内容,底部的这个额外输入也很方便。您可能还需要考虑将 .upper()
添加到 choice
检查中以使其不区分大小写。