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.
4
  • 1
    possible duplicate of What's the difference between raw_input() and input() in python3.x? Commented Mar 12, 2015 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. Commented Mar 12, 2015 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? Commented Mar 12, 2015 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? Commented Mar 12, 2015 at 18:16

1 Answer 1

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']:
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.