hmood
hmood

Reputation: 603

How to check if an element exists in a list in Python

Here is some code:

test_list = list(range(0, 100))
number = input("Enter a number between 0 and 100: ")
if (guess in test_list): 
    print ("You have entered a number between 1 and 100")

Now if I run this, it doesn't work. Just doesn't work. It leaves me with this and nothing else:

Process finished with exit code 0

What is the correct way to check if an element exists in a list?

I had found this method on a website called 'GeeksForGeeks'

This code was written in the PyCharm IDE

Upvotes: 0

Views: 277

Answers (1)

Aramakus
Aramakus

Reputation: 1920

Change guess to number and convert input string to int

test_list = list(range(0, 100))
number = input("Enter a number between 0 and 100: ")
if int(number) in test_list: 
    print ("You have entered a number between 1 and 100")

Upvotes: 3

Related Questions