1

I have this while loop that is testing to make sure the user inputs either 1, 2, or 3. Once the user inputs 1, 2, or 3, the loop keeps going infinitely, i can't get out of it.

while True:
    try:
         filterselection = raw_input("Please select a filter (1, 2, or 3): ")
         if filterselection == "1" or filterselection == "2" or filterselection == "3":
              filterselection = int(filterselection)
              break

      else:
          print "Not a valid number try again!"
      except TypeError:
          print "Lol, that's not a number try again!"
4
  • And the question is...? Commented Jun 25, 2013 at 1:46
  • 6
    Your indentation seems to be inconsistent. Commented Jun 25, 2013 at 1:49
  • 2
    instead of the multiple ors you can use the in operator and say if filterselection in ('1', '2', '3'): . for your health. Commented Jun 25, 2013 at 2:02
  • also, there's nowhere in here that a TypeError could occur. Unless you wanted to change the if to say if int(filter_selection) in range(1,4): Commented Jun 25, 2013 at 2:04

1 Answer 1

10

Don't mix tabs and spaces! Here is what I see when pasting your original code into an editor that displays whitespace characters:

enter image description here

The arrows are tabs and the dots are spaces, it is very important that you don't mix these because if you do the code that you see might not be what the Python interpreter sees.

Sign up to request clarification or add additional context in comments.

3 Comments

What's the best way to make sure code is copied correctly from Geany into this site? Because when I copied and pasted, everything looked all weird on here.
@Binka My guess is that everything looked weird because there was a mix of tabs and spaces in your code, some sites or programs will display tabs with the same width as 8 spaces, other with a width of 4 spaces.

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.