while 循环要求输入两次,即使输入位于指定列表内。我需要帮助找出更好的方法或解决此问题
我是Python新手,所以请保持友善;),我还没有真正尝试过很多,因为我不知道我还能怎么做。
Y_list = ("Y")
N_list = ("N")
user_choice = input("Y / N")
while user_choice not in Y_list or N_list:
user_choice = input("Y / N")
if user_choice in Y_list:
print("U said Y")
elif user_choice in N_list:
print("U said N")
我希望只问一次问题,除非用户输入的内容不在 Y 列表或 N 列表中
最佳答案
我想你可能想要这样的东西:
Y_list = ("y", "yes", "yeah")
N_list = ("n", "no", "none")
while True:
user_choice = input("Y / N").lower()
if user_choice in Y_list:
print("U said Y")
break
elif user_choice in N_list:
print("U said N")
break
关于python - While 循环在允许 if 语句运行之前要求输入两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57551539/