input: two integer values lower limit 'start' and maximum 'end' the arguments aren't inclusive. output: if reading successful then returns the read integer. purpose: reads from command-line a integer in the given bounds. while input invalid asks user
(start, end)
| 1 | def get_user_input(start, end): |
| 2 | """ |
| 3 | input: two integer values |
| 4 | lower limit 'start' and maximum 'end' |
| 5 | the arguments aren't inclusive. |
| 6 | |
| 7 | output: if reading successful then returns the read integer. |
| 8 | |
| 9 | purpose: reads from command-line a integer in the given bounds. |
| 10 | while input invalid asks user again |
| 11 | """ |
| 12 | |
| 13 | loop = True # controls while-loop |
| 14 | |
| 15 | while loop: |
| 16 | try: |
| 17 | # reads and converts the input from the console. |
| 18 | user_input = int(input("Enter Your choice: ")) |
| 19 | |
| 20 | # checks whether input is in the given bounds. |
| 21 | if user_input > end or user_input < start: |
| 22 | # error case |
| 23 | print("Please try again. Not in valid bounds.") |
| 24 | |
| 25 | else: |
| 26 | # valid case |
| 27 | loop = False # aborts while-loop |
| 28 | |
| 29 | except ValueError: |
| 30 | # error case |
| 31 | print("Please try again. Only numbers") |
| 32 | |
| 33 | return user_input |
| 34 | |
| 35 | |
| 36 | x = get_user_input(1, 6) |