-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_input.py
119 lines (103 loc) · 3.21 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
""" Set of functions to validate user input.
Written by: Shannon Cleary
Date: Fall 2022
Functions:
get_int(prompt) - returns a valid integer (positive or negative).
get_positive_int(prompt) - returns a valid positive (>=0) integer.
get_int_range(prompt, low, high) - returns a valid integer within the inclusive range.
get_float(prompt) - returns a valid decimal value.
get_yes_no(prompt) - returns a valid yes/no input.
Usage: in your program, import the check_input module. Then call one of the methods using check_input.
Example Usage:
import check_input
val = check_input.get_int("Enter #:")
print(val)
"""
def get_int(prompt):
"""Repeatedly takes in and validates user's input to ensure that it is an integer.
Args:
prompt: string to display to the user to prompt them to enter an input.
Returns:
The valid positive integer input.
"""
val = 0
valid = False
while not valid:
try:
val = int(input(prompt))
valid = True
except ValueError:
print("Invalid input - should be an integer.")
return val
def get_positive_int(prompt):
"""Repeatedly takes in and validates user's input to ensure that it is a positive (>= 0) integer.
Args:
prompt: string to display to the user to prompt them to enter an input.
Returns:
The valid integer input.
"""
val = 0
valid = False
while not valid:
try:
val = int(input(prompt))
if val >= 0:
valid = True
else:
print("Invalid input - should not be negative.")
except ValueError:
print("Invalid input - should be an integer.")
return val
def get_int_range(prompt, low, high):
"""Repeatedly takes in and validates user's input to ensure that it is an integer within the specified range.
Args:
prompt: string to display to the user to prompt them to enter an input.
low: lower bound of range (inclusive)
high: upper bound of range (inclusive)
Returns:
The valid integer input within the specified range.
"""
val = 0
valid = False
while not valid:
try:
val = int(input(prompt))
if val >= low and val <= high:
valid = True
else:
print("Invalid input - should be within range " + str(low) + "-" + str(high) + ".")
except ValueError:
print("Invalid input - should be an integer.")
return val
def get_float(prompt):
"""Repeatedly takes in and validates user's input to ensure that it is a float.
Args:
prompt: string to display to the user to prompt them to enter an input.
Returns:
The valid floating point input.
"""
val = 0
valid = False
while not valid:
try:
val = float(input(prompt))
valid = True
except ValueError:
print("Invalid input - should be a decimal value.")
return val
def get_yes_no(prompt):
"""Repeatedly takes in and validates user's input to ensure that it is a yes or a no answer.
Args:
prompt: string to display to the user to prompt them to enter an input.
Returns:
True if yes, False if no.
"""
valid = False
while not valid:
val = input(prompt).upper()
if val == "YES" or val == "Y":
return True
elif val == "NO" or val == "N":
return False
else:
print("Invalid input - should be a 'Yes' or 'No'.")