-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputFunctions.py
More file actions
61 lines (47 loc) · 2.13 KB
/
inputFunctions.py
File metadata and controls
61 lines (47 loc) · 2.13 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
def take_number(prompt, min_val, max_val, dilimeter=None, dil_count=0):
while True:
answer = input(prompt).strip()
if answer == '':
print('An empty string is not a valid input.\n')
continue
if dilimeter is not None:
if dilimeter not in answer:
if dil_count > 0:
if dil_count != answer.count(dilimeter):
print(f'You need to input {dil_count + 1} values separated by "{dilimeter}".\n')
continue
print(f'Your values need to be separated by {dilimeter}\n')
continue
else:
if not answer.replace(dilimeter, "").isnumeric():
print(f'Please type numbers separated by {dilimeter} not a string')
continue
answer = [int(an) for an in answer.split(dilimeter)]
if len(set(answer)) < len(answer):
print('You can not choose duplicate values. The values must be different.')
continue
elif len(answer) > dil_count + 1:
print(f'You can only type {dil_count+1} values separated by a {dilimeter}.')
continue
else:
if not answer.isnumeric():
print('Your answer must be a single number.')
continue
answer = [int(answer)]
valid_values = True
for val in answer:
if not (min_val <= val and val <= max_val):
print(f'Invalid values. Your value(s) has to be between {min_val} and {max_val}\n')
valid_values = False
break
if not valid_values:
continue
return answer
def take_command(prompt, correct_values):
while True:
answer = input(prompt).lower().strip()
if answer not in correct_values:
print(f'\nInvalid response. Your response must be one of the following:')
print(" ", *correct_values)
continue
return answer