-1

I'm getting confused as to why I cannot have a if statement within the for statement,

Going to try and clear this up sorry for not being clear.

lists = [1,2,3,4,5,6]
userList = []
for i in range(5):
    userList.append(input("Please enter a number :"))
for L in userList:
     if L in lists:
        print("It is in it")
    else:
        print("It is NOT in it")  

I would like it to print "It is in it" should a number from userList be in lists.

So any number the user enters between 1 and 6 will print "It is in it".

Also sorry for saying python 2, my mistake.

Jack
  • 2,891
  • 11
  • 48
  • 65
  • 2
    You aren't using python 3. If you were you would only be getting `"It is not in it"` because `input()` returns a string in python 3 – jamylak Apr 15 '13 at 12:23
  • 2
    You also aren't using this exact code, otherwise you'd be getting `It is NOT in it` rather than `it is not in it`. – mgilson Apr 15 '13 at 12:24
  • I tried your code, and it works for me ... – ibi0tux Apr 15 '13 at 12:25
  • 3
    But what exactly is the problem? It seems to me that the `if` within the `for` works just fine ... – mgilson Apr 15 '13 at 12:26
  • Even if you are not using this exact code, what is your question? what is wrong with your example output? – Emil Vikström Apr 15 '13 at 12:26
  • I'm sorry for not being clear, i looked over it after i got a drink and saw my mistakes after it had already been down voted. Sorry I hope i fixed this. – Jack Apr 15 '13 at 12:35
  • Possibly related: http://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x – Aya Apr 15 '13 at 12:36

2 Answers2

2

input() returns a string, not an int as you expected in the code.

Use this

userList.append(int(input("Please enter a number :")))

The int() will convert the string to an int.

arulmr
  • 8,620
  • 9
  • 54
  • 69
P_Rein
  • 500
  • 1
  • 6
  • 16
  • This doesn't matter since the comparison made by the `in` statement will compare int values as strings – ibi0tux Apr 15 '13 at 12:30
  • @ibi0tux Actually it won't. The reason it works on Python 2.x only is the `input()` is effectively doing an `eval()` on the string. In Python 3.x, the code will fail. – Aya Apr 15 '13 at 12:33
  • 1
    @ibi0tux -- incorrect. `"1" in (1,2,3)` will return `False`. – mgilson Apr 15 '13 at 12:33
  • You might want to mention that this is a change in the behavior from the `input` function in python 2.x – mgilson Apr 15 '13 at 12:34
  • I said this according to @jack is using Python 2.x. I know the behaviour is different in python 3 – ibi0tux Apr 15 '13 at 12:36
  • Yes sorry i meant to add python 3. My mistake I hit the wrong one. – Jack Apr 15 '13 at 12:37
  • 2
    @ibi0tux either way, your assertion that "the comparison made by the `in` statement will compare int values as strings" is a little misleading. – Aya Apr 15 '13 at 12:39
1

A version that would work in both Python2.X and python3.x ... somehow more complex.

lists = [1,2,3,4,5,6]
userList = []
for i in range(5):
    userList.append(input("Please enter a number :"))

print lists
print userList

for L in map(lambda x:int(x),userList):
    if L in map(lambda x:int(x),lists):
        print(L," is in it")
    else:
        print(L," is NOT in it")

Output :

Please enter a number :1
Please enter a number :3
Please enter a number :5
Please enter a number :7
Please enter a number :8
[1, 2, 3, 4, 5, 6]
[1, 3, 5, 7, 8]
1  is in it
3  is in it
5  is in it
7  is NOT in it
8  is NOT in it

It prints exactly what you want.

ibi0tux
  • 2,481
  • 4
  • 28
  • 49