-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.py
More file actions
59 lines (44 loc) · 1.48 KB
/
validation.py
File metadata and controls
59 lines (44 loc) · 1.48 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
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
#!/usr/bin/env python3
"""validation.py contains validation functions to be used to verify input data from the user"""
def ValidateUserInput(text, validChoices):
"""
Prompts the user for input until a valid choice from the provided list is entered.
Args:
text: The prompt to display to the user.
validChoices: A list of valid choices (strings).
Returns:
The user's choice (string, lowercased).
"""
if not hasattr(validChoices, '__iter__'):
raise TypeError("ValidChoices must be iterable")
userChoice = 'userChoice'
while userChoice.lower() not in validChoices:
userChoice = input(text)
if userChoice.lower() in validChoices:
break
print("That is not a valid choice")
return userChoice.lower()
def GetIntValue(text, min=0, max=100):
"""
Prompts the user for an integer value until a valid integer value is entered.
Args:
text: The prompt to display to the user.
Returns:
The integer value entered by the user.
"""
while True:
try:
intValue = int(input(f"{text}"))
assert intValue >= min and intValue <= max
break
except ValueError:
print("That is not a valid number")
except AssertionError:
print(f"Value must be between {min} and {max} ")
return intValue
if __name__ == '__main__':
userInput = ValidateUserInput(
"Please select from the following choices (y/n): ", ('y', 'n'))
print(userInput)
userInt = GetIntValue("Enter a value to verify functionality: ")
print(userInt)