阻止用户输入无效的运算符

时间:2014-11-11 02:40:33

标签: python operator-keyword

如果以下4个字母E / A / S / T是我的运营商,我该怎么做才能让用户输入无效的运营商。基本上除了下面列出的任何东西?

print("Available Operators:")
print("'E' sets starting number")
print("'A' for addition")
print("'S' for subtraction")
print("'T' will terminate program")

# Prompt user to select the operator 
choice = input("Select Operator (e/a/s/t): ")

2 个答案:

答案 0 :(得分:0)

您可以使用in来测试选项列表:

direction = input()
if direction in 'NESW':
    # valid input
else:
    # invalid input

答案 1 :(得分:0)

valid_entries = {'E', 'A', 'S', 'T'}

while True:
    result = input()
    if result in valid_entries:
        break
    print('Please enter a valid letter')