输入是一个字符串,因此您将字符串与整数进行比较.首先转换为int然后进行成员资格测试:
numbers = [1,2,3,4,5,6,7] x = input() if int(x) in numbers: print("Hey you did it") else: print("Nope")
为了使这个更加健壮,你应该处理ValueError
如果用户没有输入整数会发生的情况(总会有一个用户输入'cheeseburger'而不是数字):
numbers = [1,2,3,4,5,6,7] x = input() try: i = int(x) if i in numbers: print("Hey you did it") else: print("Nope") except ValueError: print("You did not enter a number")