Skip to content

Commit e9e317f

Browse files
authored
Update validation.py
1 parent 69260a3 commit e9e317f

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

validation.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22
"""validation.py contains validation functions to be used to verify input data from the user"""
33

4+
45
def ValidateUserInput(text, validChoices):
56
"""
67
Prompts the user for input until a valid choice from the provided list is entered.
@@ -13,19 +14,20 @@ def ValidateUserInput(text, validChoices):
1314
The user's choice (string, lowercased).
1415
"""
1516

16-
if not hasattr(validChoices,'__iter__'):
17+
if not hasattr(validChoices, '__iter__'):
1718
raise TypeError("ValidChoices must be iterable")
1819

19-
userChoice='userChoice'
20+
userChoice = 'userChoice'
2021
while userChoice.lower() not in validChoices:
21-
userChoice=input(text)
22+
userChoice = input(text)
2223
if userChoice.lower() in validChoices:
2324
break
2425
print("That is not a valid choice")
2526

2627
return userChoice.lower()
2728

28-
def GetIntValue(text):
29+
30+
def GetIntValue(text, min=0, max=100):
2931
"""
3032
Prompts the user for an integer value until a valid integer value is entered.
3133
@@ -37,16 +39,21 @@ def GetIntValue(text):
3739
"""
3840
while True:
3941
try:
40-
intValue=int(input(f"{text}"))
41-
break
42+
intValue = int(input(f"{text}"))
43+
assert intValue >= min and intValue <= max
44+
break
4245
except ValueError:
43-
print("That is not a valid number")
46+
print("That is not a valid number")
47+
except AssertionError:
48+
print(f"Value must be between {min} and {max} ")
4449

4550
return intValue
4651

52+
4753
if __name__ == '__main__':
48-
userInput=ValidateUserInput("Please select from the following choices (y/n): ", ('y','n'))
54+
userInput = ValidateUserInput(
55+
"Please select from the following choices (y/n): ", ('y', 'n'))
4956
print(userInput)
5057

51-
userInt=GetIntValue("Enter a value to verify functionality: ")
58+
userInt = GetIntValue("Enter a value to verify functionality: ")
5259
print(userInt)

0 commit comments

Comments
 (0)