Select to view content in your preferred language

# User Input Matching a List

2851
2
08-23-2017 08:58 AM
Occasional Contributor
n = [1, 7, 14, 16,19]
x = raw_input()

if x in n:
print "match"
else:
print "no match"

How can I get this to work correctly ?

1 Solution

Accepted Solutions
MVP Emeritus

Raw_input is python 2.7, it has been replaced with 'input' in python 3.x, so substitute as needed.

The reason why it isn't working is that the return value is always a string, and you are trying to see if it is within a list of numbers.... note the type conversion in the following example.

a = [1, 2, 3, 4]

x = input('input please ')  # raw_input in python 2.7

input please 1   # here I put in a number 1

x                # to confirm
'1'              # but it is a string... what to due

if int(x) in a:  # a type conversion perhaps
print(True)
else:
print(False)

True             # all is good
2 Replies
MVP Emeritus

Raw_input is python 2.7, it has been replaced with 'input' in python 3.x, so substitute as needed.

The reason why it isn't working is that the return value is always a string, and you are trying to see if it is within a list of numbers.... note the type conversion in the following example.

a = [1, 2, 3, 4]

x = input('input please ')  # raw_input in python 2.7

input please 1   # here I put in a number 1

x                # to confirm
'1'              # but it is a string... what to due

if int(x) in a:  # a type conversion perhaps
print(True)
else:
print(False)

True             # all is good
Occasional Contributor

Thanks, it works perfect now.

I don't know why I missed that.

Now to try and figure out a way to save this as standalone tool.