-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputPromptValidationModule.py
More file actions
87 lines (64 loc) · 3.39 KB
/
inputPromptValidationModule.py
File metadata and controls
87 lines (64 loc) · 3.39 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
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
# Author: Prof. Brian Candido
# Purpose: Put user input and validation into a module for sharing and reuse
#
# This module provides functions to allow the input and data validation for
# -float value
# -int value
# -string value
#################################################################################
# inputFloatValidation Function #
# Parameter 1: Is the Prompt value that user will see on the screen #
# Parameter 2: Indicates what the minimum value allowed is for example 0 or .01 #
#################################################################################
def inputFloatValidation(sPrompt, nMinNumberAllowed):
# Testing user input with while loop displaying error message if invalid
# Default fValue to -1 to force entry into the loop:
fValue = -1
# Keep Looping until a valid float that was >= to the Minimun Number that was supplied:
while fValue < nMinNumberAllowed:
try:
fValue = float(input(sPrompt))
if fValue < nMinNumberAllowed:
print ("Input must be a numeric value greater or equal to: ", nMinNumberAllowed)
continue
except ValueError:
print ("Input must be a numeric value greater or equal to: ", nMinNumberAllowed)
continue
# If code reaches here a valid float numeric value that was > then the nMinNumberAllowed
return fValue
#################################################################################
# inputIntValidation Function #
# Parameter 1: Is the Prompt value that user will see on the screen #
# Parameter 2: Indicates what the minimum value allowed is for example 0 or .01 #
#################################################################################
def inputIntValidation(sPrompt, nMinNumberAllowed):
# Testing user input with while loop displaying error message if invalid
# Default iValue to -1 to force entry into the loop:
iValue = -1
# Keep Looping until a valid float that was >= to the Minimun Number that was supplied:
while iValue < nMinNumberAllowed:
try:
iValue = int(input(sPrompt))
if iValue < nMinNumberAllowed:
print ("Input must be a numeric value greater or equal to: ", nMinNumberAllowed)
continue
except ValueError:
print ("Input must be a numeric value greater or equal to: ", nMinNumberAllowed)
continue
# If code reaches here a valid int numeric value that was > then the nMinNumberAllowed
return iValue
#################################################################################
# inputStringValidation Function #
# Parameter 1: Is the Prompt value that user will see on the screen #
#################################################################################
def inputStringValidation(sPrompt):
# Default sValue to -1 to force entry into the loop:
sValue = ""
# Keep Looping until a valid float that was >= to the Minimun Number that was supplied:
while sValue == "":
sValue = input(sPrompt)
if sValue == "":
print ("Please supply an input value.")
continue
# If code reaches here a valid string was entered
return sValue