0
nums = [1, 2, 3, 4]

choice = input()

    if choice.isnumeric() and choice in nums:
        print("First")
   
    else:
        print("Second")

It prints out Second, can anyone explain why? I assume because it reads the number as a string since its input() rather than int(input()), and then it's false because list members are int, if that's the case, is it better to write the numbers in the list as strings, or is there another way to make it work?

TERMINATOR
  • 1,180
  • 1
  • 11
  • 24

2 Answers2

0

you can cast choice to int after and:

nums = [1, 2, 3, 4]
choice = input()

if choice.isnumeric() and int(choice) in nums:
    print("First")
else:
    print("Second")
Tugay
  • 2,057
  • 5
  • 17
  • 32
0

I assume because it reads the number as a string since its input() rather than int(input()), and then it's false because list members are int

That's right. But you can convert input value to int to make it work:

if choice.isnumeric() and int(choice) in nums:
    print("First")

else:
    print("Second")
PookyFan
  • 785
  • 1
  • 8
  • 23