-1

So i created a list like this:

my_list = [1, 3, 5]
number = input("Choose a number from 1 to 5: ")

Now I want to say: if the number is inside the list: print("ERROR"), if the number is not inside the list: print(number)

So if I type 2 it will print me the 2, if I type 1 it will print me ERROR

How am I doing that?

Valeriy
  • 1,365
  • 3
  • 18
  • 45
Aleksandar
  • 17
  • 1

2 Answers2

1

I think your looking something like the below example

my_list = [1, 3, 5]
number = input("Choose a number from 1 to 5: ")
if int(number) in my_list:
    print("error. . . . ")
else:
    print(int(number))

And when you run it this is how it works

Choose a number from 1 to 5: 2

2

Choose a number from 1 to 5: 1

error. . . .

Chetan_Vasudevan
  • 2,414
  • 1
  • 13
  • 34
1

First you need to take user input as int and then check if the number entered by user is present in my_list or not.

my_list = [1, 3, 5]
number = int(input("Choose a number from 1 to 5: "))


if number in my_list:
  print('Error')
else:
  print(number)