Python 数据验证未按预期工作
Python data validation not working as intended
Python 3.8.12
此代码的预期目标是允许用户select“牛肉”、“鸡肉”、“豆腐”或“none”三明治。如果用户没有输入这些选项之一,它将再次提示他们 select 一个三明治。如果他们确实输入了这些选项之一,那么它将继续使用代码。
无法正常工作。它不会接受任何有效或无效的输入。所有输入都会导致程序再次提示用户,而不是在有效时继续执行程序。
sandwich_choice = input("Select sandwich: ")
while sandwich_choice != "chicken" or sandwich_choice != "beef" or sandwich_choice != "tofu" or sandwich_choice != "none":
sandwich_choice = input("\x1b[30;41m[!]\x1b[0mSelect sandwich: ")
else:
pass
print("Sandwich selection is", sandwich_choice)
更好的方法是:
sandwich_choice = ""
while True:
sandwich_choice = input("blah blah blah")
if sandwich_choice == "beef" or sandwich_choice == "chicken" or sandwich_choice == "tofu" or sandwich_choice == "none":
break
print("Sandwich selection is",sandwich_choice)
修改后的逻辑:
sandwich_choice = input("Select sandwich: ")
while sandwich_choice not in ('chicken', 'beef', 'tofu', 'none'):
sandwich_choice = input("\x1b[30;41m[!]\x1b[0mSelect sandwich: ")
else:
pass
print("Sandwich selection is", sandwich_choice)
你可以试试
sandwich_choice = input("Select sandwich: ")
list_sandwich = ['chicken', 'beef', 'tofu', 'none']
while sandwich_choice not in list_sandwich:
sandwich_choice = input("\x1b[30;41m[!]\x1b[0mSelect sandwich: ")
else:
pass
print("Sandwich selection is", sandwich_choice)
基于Carl_M的实施
sandwich_choice = input("Select sandwich: ")
while sandwich_choice not in ('chicken', 'beef', 'tofu', 'none'):
sandwich_choice = input("\x1b[30;41m[!]\x1b[0mSelect sandwich: ")
else:
print("Sandwich selection is", sandwich_choice)
Python 3.8.12
此代码的预期目标是允许用户select“牛肉”、“鸡肉”、“豆腐”或“none”三明治。如果用户没有输入这些选项之一,它将再次提示他们 select 一个三明治。如果他们确实输入了这些选项之一,那么它将继续使用代码。
无法正常工作。它不会接受任何有效或无效的输入。所有输入都会导致程序再次提示用户,而不是在有效时继续执行程序。
sandwich_choice = input("Select sandwich: ")
while sandwich_choice != "chicken" or sandwich_choice != "beef" or sandwich_choice != "tofu" or sandwich_choice != "none":
sandwich_choice = input("\x1b[30;41m[!]\x1b[0mSelect sandwich: ")
else:
pass
print("Sandwich selection is", sandwich_choice)
更好的方法是:
sandwich_choice = ""
while True:
sandwich_choice = input("blah blah blah")
if sandwich_choice == "beef" or sandwich_choice == "chicken" or sandwich_choice == "tofu" or sandwich_choice == "none":
break
print("Sandwich selection is",sandwich_choice)
修改后的逻辑:
sandwich_choice = input("Select sandwich: ")
while sandwich_choice not in ('chicken', 'beef', 'tofu', 'none'):
sandwich_choice = input("\x1b[30;41m[!]\x1b[0mSelect sandwich: ")
else:
pass
print("Sandwich selection is", sandwich_choice)
你可以试试
sandwich_choice = input("Select sandwich: ")
list_sandwich = ['chicken', 'beef', 'tofu', 'none']
while sandwich_choice not in list_sandwich:
sandwich_choice = input("\x1b[30;41m[!]\x1b[0mSelect sandwich: ")
else:
pass
print("Sandwich selection is", sandwich_choice)
基于Carl_M的实施
sandwich_choice = input("Select sandwich: ")
while sandwich_choice not in ('chicken', 'beef', 'tofu', 'none'):
sandwich_choice = input("\x1b[30;41m[!]\x1b[0mSelect sandwich: ")
else:
print("Sandwich selection is", sandwich_choice)