-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_input_handler.py
45 lines (41 loc) · 1.74 KB
/
user_input_handler.py
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
class UserInput:
def ask_for_free_text(statement: str) -> str:
return input(statement + "\n")
def ask_for_integer(statement: str, min: int, max: int) -> int:
while True:
try:
data = int(input(f"{statement}\nPossible answers are between {min} and {max}.\n"))
if not min <= data <= max:
print("Please input a whole number.")
except ValueError:
print("Please input a whole number.")
else:
break
return data
def ask_for_boolean(statement: str) -> bool:
while True:
try:
data = input(f"{statement}\nPossible answers:\n yes\n no\n")
if data != "yes" and data != "no":
print(f"Please answer either 'yes' or 'no'.")
continue
except ValueError:
print("Please input a whole number.")
else:
break
return data == "yes"
def ask_to_select(statement: str, options: list[str], return_index: bool=False) -> str:
statement = f"{statement}\nPossible answers:\n"
for i, option in enumerate(options, start=1):
statement += f" {i}. {option}\n"
while True:
try:
index = int(input(statement))
if index > len(options) + 1:
print(f"There is no answer option for the number {index}.")
continue
except ValueError:
print("Please input the number of your answer.")
else:
break
return index - 1 if return_index else options[index - 1]