MCPcopy Create free account
hub / github.com/geekcomputers/Python / get_user_input

Function get_user_input

check_input.py:1–33  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

1def 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
36x = get_user_input(1, 6)

Callers 1

check_input.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected