#这是我正在编写的程序。我希望它一直持续到我按“n”。但它只工作一次。任何答案将不胜感激。谢谢
names = ["Steve"]
add = input("Enter your name: ")
names.append(add)
ques = input("Do you want to add more? (Press 'y' for yes and 'n' for no)")
if(ques == "y"):
add = input("Enter your name: ")
names.append(add)
print(*names, sep="\n")
elif(ques == "n"):
print("Program Stopped")
else:
print("Entered wrong letter!")
你需要一个无限while循环的是,一定要制作break
的无限 循环,一旦你在elif
或else
块,我也优化区分大小写的比较,引起最终用户可以按下Y
不y
,同样,对于n
和'N”。
代码 :
while True:
names = ["Steve"]
add = input("Enter your name: ")
names.append(add)
ques = input("Do you want to add more? (Press 'y' for yes and 'n' for no)")
if ques == "y" or ques =='Y':
add = input("Enter your name: ")
names.append(add)
print(*names, sep="\n")
elif ques == "n" or ques =='N':
print("Program Stopped")
break
else:
print("Entered wrong letter!")
break
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句