python - While 循环在允许 if 语句运行之前要求输入两次

标签 python loops while-loop

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/

相关文章:

python - 在 for 循环函数中以字符串形式调用变量名

python - while 循环计数器

Java while 循环测验不会重置

python - set.union() 提示它在传入生成器时没有参数

python - 如何对相似的文件路径进行聚类?

python - odoo 10 后端开发

java - 用jprogressbar显示剩余时间?

python - 内容处置不起作用

c++ - 手动解决此程序时我得到不同的输出

java - 在嵌套 while 循环中将迭代器位置重置为 0