0

I'm writing a simple program for something, and I have some lines that go

helpme = input("Select a number:")

if helpme in [1, 2, 3, 4, 5]:
  print ("Okay then.")
elif helpme == 6:
  print ("Are you sure?")
else:
  print ("Please enter a valid option")

However, no matter the input for the variable "helpme", the code always returns the "Please enter a valid option" line. Am I doing something wrong? I have already attempted using individual elif statements for each number, and also using a print statement to print the value of "helpme" to check it, and everything looks like it should run fine.

Kenny
  • 3
  • 3

2 Answers2

1

This should work, just need to explicitly cast the input to an int.

helpme = int(input("Select a number:"))

if helpme in [1, 2, 3, 4, 5]:
  print ("Okay then.")
elif helpme == 6:
  print ("Are you sure?")
else:
  print ("Please enter a valid option")
hysoftwareeng
  • 497
  • 4
  • 15
0

helpme is a string while you are comparing to integers. Either make helpme an int:

helpme = int(input("Select a number:"))

if helpme in [1, 2, 3, 4, 5]:
  print ("Okay then.")
elif helpme == 6:
  print ("Are you sure?")
else:
  print ("Please enter a valid option")

Or compare to strings:

helpme = input("Select a number:")

if helpme in ["1", "2", "3", "4", "5"]:
  print ("Okay then.")
elif helpme == "6":
  print ("Are you sure?")
else:
  print ("Please enter a valid option")
Daniel Centore
  • 3,220
  • 1
  • 18
  • 39