0

I only want the inputs 1000 to 1700. The except block is able to catch the error if the input is not an integer and rerun the try block but not if the input is an integer BUT is not the one I want. How can I rerun the try block until the user enters the correct integers? Thanks in advance!

def gettriptime():
    while True:
        try:
            ch=int(input("""
Trip Times:
1000
1100
1200
1300
1400
1500
1600
1700

Please enter trip time: """))
            if (ch==1000) or (ch==1100) or (ch==1200) or (ch==1300) or (ch==1400) or (ch==1500) or (ch==1600):
                        return ch
            else:
                print("\nInvalid trip time.")
        except ValueError:
                print("\nPlease enter a valid option. ")
        else:
            break

gettriptime()
Jay Man
  • 31
  • 6
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – tripleee Jun 17 '18 at 06:46

2 Answers2

4

There is no need of try-except blocks. Check if user input is a number in list and if so, exit the infinite loop:

def gettriptime():
    lst = [1000, 1100, 1200, 1300 , 1400, 1500, 1600, 1700]
    while True:
        ch = int(input("""
Trip Times:
1000
1100
1200
1300
1400
1500
1600
1700
Please enter trip time: """))
        if ch in lst:
            return ch

ch = gettriptime()

Or even better (without creating a list of your own) take advantage of range:

def gettriptime():
    while True:
        ch = int(input("""
Trip Times:
1000
1100
1200
1300
1400
1500
1600
1700
Please enter trip time: """))
        if ch in range(1000, 1800, 100):
            return ch

ch = gettriptime()

Also you may note to get the returned value to a variable, otherwise it's wasted. gettriptime() -> ch = gettriptime()

Austin
  • 25,759
  • 4
  • 25
  • 48
  • 1
    Nice answer Austin, specially the last one.This deserves my upvote. – Taohidul Islam Jun 17 '18 at 06:39
  • I think the try-except block is need because if omitted an error will occur when the user enters a string instead of an integer. Regardless, this solved my problem. Many thanks!' – Jay Man Jun 17 '18 at 07:48
  • I assumed only integer inputs. I was focusing more on handling the range part. To handle other type cases, of course, you need a `try-except` as in your code. Anyways, glad it helped you. :) – Austin Jun 17 '18 at 08:09
1

Just remove the extra else block. When user enters wrong input then your last else block becomes executed and breaks the loop. You just need to remove it.

def gettriptime():
    while True:
        try:
            ch=int(input("""
Trip Times:
1000
1100
1200
1300
1400
1500
1600
1700

Please enter trip time: """))
            if (ch==1000) or (ch==1100) or (ch==1200) or (ch==1300) or (ch==1400) or (ch==1500) or (ch==1600):
                        return ch
            else:
                print("\nInvalid trip time.")
        except ValueError:
                print("\nPlease enter a valid option. ")


gettriptime()

Note: You can remove your try/except block. It's not necessary here, As Austin suggested.

Taohidul Islam
  • 5,246
  • 3
  • 26
  • 39