1

The issue I'm having is that when the user inputs the classfile, it will keep say it is an invalid input. Any idea why this happening?

classfile = input("Which class would you like to display: ") #Prompts the user to find out which wile to open
while classfile not in [1, 2, 3]: #Ensures the input it valid
    print("There are only classes 1, 2 and 3 available.")
    classfile = input("Which class would you like to display: ") #If input is not valid it will ask them to input it again.
Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119
Alex Raj
  • 23
  • 3
  • 1
    possible duplicate of [What's the difference between raw\_input() and input() in python3.x?](http://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x) – runDOSrun Mar 12 '15 at 18:04
  • @runDOSrun That is a very unclear duplicate. The OP nowhere mentions `raw_input` and may not even know about it if they have only worked in Python 3. – Two-Bit Alchemist Mar 12 '15 at 18:13
  • @Two-BitAlchemist Maybe you're right but the link contains everything OP needs to know to solve the issue, doesn't it? – runDOSrun Mar 12 '15 at 18:13
  • 1
    @EricFortin The fact that they wouldn't be having the described problem in Python 2 with the posted code isn't clear enough? – Two-Bit Alchemist Mar 12 '15 at 18:16

1 Answers1

2

input in Python 3 returns a string. Your while-statement compares this string to integers. This won't work because strings never compare equal to integers.

You can fix this by casting your input to an integer or by comparing it to strings. I prefer the latter because then you won't get an exception on non-integer input.

So change your while-statement to the following and your code will work:

while classfile not in ['1', '2', '3']:
Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119