如何检查用户输入是否是我作为选项给出的确切字符串
How to check if user input is an exact string I gave as an option
用户可以选择是否选择字符串。我只想确保用户选择我提供的特定字符串。
我的代码如下所示:
category_choice = input("Which category would you like to search with? Here are the choice: \n"
"'Name', 'Phone number','Class time','Class duration'\n")
while category_choice != "Name" or "Phone number" or "Class time" or "Class duration":
category_choice = input("Please specifically write one of the choices of the list.\nInput: ")
一旦用户输入正确,我希望它退出 while 循环并继续,但它似乎永远持续下去。
我做错了什么?
您必须更改条件:
while category_choice not in ["Name", "Phone number", "Class time", "Class duration"]:
或
while category_choice != "Name" or category_choice != "Phone number" or category_choice != "Class time" or category_choice != "Class duration":
尝试
category_choice = input("Which category would you like to search with? Here are the choice: \n""'Name', 'Phone number','Class time','Class duration'\n")
while category_choice not in ["Name","Phone number","Class time","Class duration"]:
category_choice = input("Please specifically write one of the choices of the list.\nInput: ")
当你写一个孤立的字符串时,如'Name'或'Phone'或'Other thing',return值将为True,因为bool('anyword') == True, and bool("") == False
while category_choice != "Name" or "Phone number" or "Class time" or "Class duration":
尝试打印此条件以检查它 returns 是真还是假。输入时,只检查第一个条件,其余的将 category_choice 分配为“Phone number”。
while category_choice != "Name" and category_choice != "Phone number" and category_choice != "Class time" and category_choice != "Class duration":
category_choice = input("Please specifically write one of the choices of the list.\nInput: ")
您应该使用 and 而不是 or ,因为输入值将始终为“姓名”、“Phone 号码”、“Class 时间”、“Class 持续时间”。让输入为 category_choice as "Name 'when it checks the condition using or that condition will always true ,because category_choice != "Phone number", similarly for other cases.
希望对您有所帮助
用户可以选择是否选择字符串。我只想确保用户选择我提供的特定字符串。
我的代码如下所示:
category_choice = input("Which category would you like to search with? Here are the choice: \n"
"'Name', 'Phone number','Class time','Class duration'\n")
while category_choice != "Name" or "Phone number" or "Class time" or "Class duration":
category_choice = input("Please specifically write one of the choices of the list.\nInput: ")
一旦用户输入正确,我希望它退出 while 循环并继续,但它似乎永远持续下去。
我做错了什么?
您必须更改条件:
while category_choice not in ["Name", "Phone number", "Class time", "Class duration"]:
或
while category_choice != "Name" or category_choice != "Phone number" or category_choice != "Class time" or category_choice != "Class duration":
尝试
category_choice = input("Which category would you like to search with? Here are the choice: \n""'Name', 'Phone number','Class time','Class duration'\n")
while category_choice not in ["Name","Phone number","Class time","Class duration"]:
category_choice = input("Please specifically write one of the choices of the list.\nInput: ")
当你写一个孤立的字符串时,如'Name'或'Phone'或'Other thing',return值将为True,因为bool('anyword') == True, and bool("") == False
while category_choice != "Name" or "Phone number" or "Class time" or "Class duration":
尝试打印此条件以检查它 returns 是真还是假。输入时,只检查第一个条件,其余的将 category_choice 分配为“Phone number”。
while category_choice != "Name" and category_choice != "Phone number" and category_choice != "Class time" and category_choice != "Class duration":
category_choice = input("Please specifically write one of the choices of the list.\nInput: ")
您应该使用 and 而不是 or ,因为输入值将始终为“姓名”、“Phone 号码”、“Class 时间”、“Class 持续时间”。让输入为 category_choice as "Name 'when it checks the condition using or that condition will always true ,because category_choice != "Phone number", similarly for other cases.
希望对您有所帮助