-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputvalidation.py
More file actions
26 lines (22 loc) · 954 Bytes
/
Copy pathinputvalidation.py
File metadata and controls
26 lines (22 loc) · 954 Bytes
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
import numbers
def getInput(prompt="", cast=None, condition=None, errorMessage=None):
while True:
try:
response = (cast or str)(input(prompt))
assert condition is None or condition(response)
return response
except:
print(errorMessage or "Invalid input. Please try again")
name = getInput(prompt="Enter your name: ")
age = getInput(prompt="Enter your age: ",
cast = int,
condition=lambda x: x > 0)
height = getInput(prompt="Enter your height in inches: ",
cast=float,
condition=lambda x: x > 0,
errorMessage="That's not a valid input")
numbers = getInput(prompt="Enter integers: ",
cast=lambda line: list(map(int, line.split(" "))),
errorMessage="Invalid input. Make sure to separate with a single space.")
print(name, age, height)
print(numbers, sum(numbers))