-2
numbers = [1,2,3,4,5,6,7]
x = input()
if x in numbers:
    print("Hey you did it")
else:
    print("Nope")

I'm not sure what I'm doing wrong here, but it always tells me that my number is not in the list.. even though it is. Works fine with strings, though.

Help would be appreciated. Thanks!

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
Mothrakk
  • 70
  • 1
  • 9

1 Answers1

5

Input is a string, so you are comparing strings to integers. First convert to an int then do the membership test:

numbers = [1,2,3,4,5,6,7]
x = input()
if int(x) in numbers:
    print("Hey you did it")
else:
    print("Nope")

To make this slightly more robust, you should handle the ValueError that will occur if the user does not input an integer (there's always one user who will enter 'cheeseburger' instead of a number):

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")
Dan
  • 4,488
  • 5
  • 48
  • 75
  • @Mothrakk glad to hear that. If this solves your problem completely, please mark it as the accepted answer so that other users know that the issue has been solved. – Dan Dec 24 '15 at 04:39
  • To be honest, the `[duplicate]` tag already lets other users know that the issue has been solved (this is a very common question, too). – TigerhawkT3 Dec 24 '15 at 04:48
  • @TigerhawkT3 10-4, didn't realize it was a dup (but should have known). Thanks. – Dan Dec 24 '15 at 04:49