while 循环输入 Yes/No (Python 3)

while loop input Yes/No (Python 3)

这是代码:

quiz = str(input("would you like to answer some questions \n choose y/n"))

quiz = quiz.lower()
while quiz != 'y' or quiz != 'n':
    print("please choose 'y' or 'n'")
    input("y/n?")

这是我的代码的一部分我尝试使用 str() 即使没有 or 操作数它也不起作用顺便说一句我正在使用 python v3.7.

1) 如果可以请修改代码

2) 如果您知道其他一些代码更有效,请告诉

注:如果输入是y。 # 例如 错误是 " ValueError: float: Argument: y is not a number "

输入查找整数,原始输入将接收字符串。尝试使用

raw_input(y/n)

这应该清除 ValueError

也许是这样的?

def check_input(predicate, msg, error_string="Illegal Input"):
    while True:
        result = input(msg).strip()
        if predicate(result):
            return result
        print(error_string)

result = check_input(lambda x: x in ['yes', 'no'],
                                   'Yes or no?')
print(result)

盗自 here

//This code is for explanation 

quiz = str(input("would you like to answer some questions \n choose y/n"))

quiz = quiz.lower()

while quiz != 'y' and quiz != 'n': //here you are using != that will result false this logic works fine with !

//while quiz == 'y' or quiz == 'n': //will work for or

print("please choose 'y' or 'n'")

input("y/n?")

//this code will work 

quiz = str(input("would you like to answer some questions \n choose y/n"))

quiz = quiz.lower()

while quiz != 'y' and quiz != 'n':

   print("please choose 'y' or 'n'")

   input("y/n?")

希望对您有所帮助。

您可能对流程图或算法感兴趣,请关注此link:https://www.edrawsoft.com/explain-algorithm-flowchart.php

您可能也有兴趣了解 python 更多信息,请关注此 link:https://www.python.org/about/gettingstarted/

谢谢。