-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.py
46 lines (37 loc) · 1.7 KB
/
score.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
46
import utils
import input_validation
def add_score(difficutly_number: int) -> None:
points: int = (difficutly_number * 3) + 5
with open(utils.scores_file_name, 'r+', encoding='ascii') as scores_file:
score_file_value: str = scores_file.read()
if score_file_value:
previous_score: int = 0
try:
# parse save file
previous_score = int(score_file_value)
except ValueError:
# save file is corrupted, let user choose between clearing the score file or quitting the program
options: tuple[str, ...] = ('y', 'yes', 'n', 'no')
default_option: str = 'n'
while True:
user_input: str = input(
'Score file is corrupted, would you like to clear it? [yes/No]: ')
user_selection = input_validation.validate_str_in_list_case_insensitive(
user_input, options, default_option, input_not_in_list_prompt='Invalid input.')
if user_selection is None:
break
match user_input.casefold():
case 'y' | 'yes':
# clear file and set score to 0
scores_file.write('')
previous_score = 0
break
case 'n' | 'no':
print('Quitting.')
quit()
else:
previous_score: int = 0
score: int = previous_score + points
scores_file.seek(0)
scores_file.write(str(score))
scores_file.truncate()