-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_io.py
More file actions
40 lines (36 loc) · 941 Bytes
/
user_io.py
File metadata and controls
40 lines (36 loc) · 941 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Prints the prompt and gets input from the user
# Only accepts an integer
def int_input(prompt=None):
value = 0
while True:
if prompt != None:
print(prompt)
try:
value = int(input("> "))
break
except ValueError:
print("Error: Enter an integer value")
return value
# Prints the prompt and gets input from the user
# Only accepts a float
def float_input(prompt):
value = 0
while True:
print(prompt)
try:
value = float(input("> "))
break
except ValueError:
print("Error: Enter a number")
return value
# Prompts the user to enter either Y or N
# Returns True on Y and False on N
def bool_input(prompt):
choice = "Q"
while choice not in ("Y", "N"):
print(prompt)
choice = input("> ")
if choice == "Y":
return True
else:
return False