User Input Matching a List

1039
2
Jump to solution
08-23-2017 08:58 AM
DevinUnderwood2
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 ?

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

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

View solution in original post

2 Replies
DanPatterson_Retired
MVP Esteemed Contributor

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
DevinUnderwood2
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.

0 Kudos