forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_input.py
45 lines (30 loc) · 1.19 KB
/
check_input.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def get_user_input(start, end):
"""
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 again
"""
loop = True # controls while-loop
while loop:
try:
# reads and converts the input from the console.
user_input = int(input("Enter Your choice: "))
# checks whether input is in the given bounds.
if user_input > end or user_input < start:
# error case
print("Please try again. Not in valid bounds.")
else:
# valid case
loop = False # aborts while-loop
except ValueError:
# error case
print("Please try again. Only numbers")
return user_input
x = get_user_input(1, 6)
print(x)
# Asks user to enter something, ie. a number option from a menu.
# While type != interger, and not in the given range,
# Program gives error message and asks for new input.