-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbetterinputfunc.py
More file actions
33 lines (27 loc) · 1 KB
/
betterinputfunc.py
File metadata and controls
33 lines (27 loc) · 1 KB
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
# get_int_in_range(first, last)
# Forces the user to enter an integer within a
# specified range
# first is either a minimum or maximum acceptable value
# last is the corresponding other end of the range,
# either a maximum or minimum value
# Returns an acceptable value from the user
def get_int_in_range(first, last):
# If the larger number is provided first,
# switch the parameters
if first > last:
first, last = last, first
# Insist on values in the range first....last
in_value = int(input("Please enter values in the range" + str(first) + "....." + str(last) + ": "))
while in_value < first or in_value > last:
print(in_value,"is not in the range", first, "....", last)
in_value = int(input("Please try again:"))
# in_value at this point is guaranteed to be within range
return in_value;
# main
# tests the get_int_in_range function
def main():
print(get_int_in_range(10,20))
print(get_int_in_range(20,10))
print(get_int_in_range(5,5))
print(get_int_in_range(-100,100))
main() #Run the program