我正在为某事编写一个简单的程序,并且有几行内容
helpme = input("Select a number:")
if helpme in [1, 2, 3, 4, 5]:
print ("Okay then.")
elif helpme == 6:
print ("Are you sure?")
else:
print ("Please enter a valid option")
但是,无论变量“ helpme”的输入是什么,代码始终返回“请输入有效选项”行。难道我做错了什么?我已经尝试过为每个数字使用单独的elif语句,并且还使用print语句来打印“ helpme”的值以进行检查,一切看起来应该正常运行。
答案 0 :(得分:1)
这应该有效,只需要将输入显式转换为int即可。
helpme = int(input("Select a number:"))
if helpme in [1, 2, 3, 4, 5]:
print ("Okay then.")
elif helpme == 6:
print ("Are you sure?")
else:
print ("Please enter a valid option")
答案 1 :(得分:0)
helpme是与整数进行比较时的字符串。要么将helpme设为int:
helpme = int(input("Select a number:"))
if helpme in [1, 2, 3, 4, 5]:
print ("Okay then.")
elif helpme == 6:
print ("Are you sure?")
else:
print ("Please enter a valid option")
或与字符串比较:
helpme = input("Select a number:")
if helpme in ["1", "2", "3", "4", "5"]:
print ("Okay then.")
elif helpme == "6":
print ("Are you sure?")
else:
print ("Please enter a valid option")