这是代码:
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 "
答案 0 :(得分:1)
输入查找整数,原始输入将接收字符串。尝试使用
raw_input(y/n)
这应该清除ValueError
答案 1 :(得分:0)
也许是这样吗?
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被盗
答案 2 :(得分:0)
//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?")
我希望这会有所帮助。
您可能对流程图或算法感兴趣,请点击以下链接:https://www.edrawsoft.com/explain-algorithm-flowchart.php
您可能还想进一步了解python,请点击以下链接:https://www.python.org/about/gettingstarted/
谢谢。