If 语句不适用于 raw_input 提示

If statement not working for raw_input prompt

显示内容中遗漏的所有内容都是正确的,因为我之前对其进行了测试...

无论我输入什么,它仍然显示 "That is not a choice" 这是我的 else 语句

1 = 选择 1

2 = 选择 2

3 = 选择 3

while True:
    choice = raw_input("->")
    if choice == 1:
        dochoice1
        break
    elif choice == 2:
        dochoice2
        break
    elif choice == 3:
        dochoice3
        break
    else:
        print "That Is Not A Choice"
        continue

raw_input returns 一个字符串,您要将其与整数进行比较,将 choice 转换为 int,或将其与字符串进行比较:

choice = int(raw_input("->"))

或:

if choice == "1":

如果用户输入的内容无效 int,您可以捕获异常:

try:
    choice = int(raw_input("->"))
except ValueError:
    print "Invalid int"
    continue