-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_quiz.py
145 lines (115 loc) · 4.7 KB
/
math_quiz.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import time
import os
from assignments_lvl1_add import set1
from assignments_lvl2_add import set2
class MathQuiz:
def __init__(self):
# Output data
self.correct_answers = 0
self.incorrect_answers = []
self.percentage_answers = ""
self.time_taken = ""
def run_quiz(self, df):
try:
# Start timer
start_time = time.time()
# Print out assignments and take user answers
for key, value in df.items():
user_answer = int(input(f"{key}+{value}= "))
# If answer is correct, add "1" to the counter
if user_answer == key+value:
self.correct_answers += 1
# If answer is wrong, add full assignment to incorrect answers
else:
self.incorrect_answers.append(
f"Incorret answer: {key}+{value}={user_answer} Correct answer: {key+value}")
# Stop timer
end_time = time.time()
# How many time does it take
self.time_taken = end_time - start_time
# % of the all answers
self.percentage_answers = \
self.correct_answers / len(df.keys()) * 100
# If user make mistake, don't stop the game
except(ValueError, TypeError):
pass
return "You did it!"
def show_results(self):
try:
print(f"\n-------Results-------")
print(
f"You have: {self.correct_answers} from {len(set1.keys())} correct answers!")
for answer in self.incorrect_answers:
print(answer)
print(f"{round(self.percentage_answers, 2)}% from 100%")
return f"It takes: {round(self.time_taken, 2)} Sek."
except TypeError as e:
pass
def export_results(self):
try:
export = input("Do you want to export your Progress?[y/n]:")
if export == "y":
name = input("Input name for your file please: ").strip()
print("File is automatically saved to desktop")
directory = os.path.expanduser(f"~/Desktop/{name}+.txt")
with open(directory, "w") as file:
file.write(f"-------Results-------")
file.write(
f"\n{self.correct_answers} from {len(set1.keys())} correct answers!")
for answer in self.incorrect_answers:
file.write(f"\n{answer}")
file.write(
f"\n{round(self.percentage_answers, 2)}% from 100%")
file.write(f"\nIt takes: {round(self.time_taken, 2)} Sek.")
elif export == "n":
return "See you :)!"
else:
raise IndexError("You can only choose between y/n")
except IndexError as e:
print(f"Error occured! {e}")
except TypeError as e:
print(f"Error occured! {e}")
return "Successfuly exported!\n"
def main():
while True:
try:
print("Main Menu:\
\n[1] First Level of Difficulty Addition\
\n[2] Second Level of Difficulty Addition\
\n[3] Exit")
category = int(input())
# Create a quiz
math_quiz = MathQuiz()
if category == 1:
start = input(
"It can take from 2 to 10 minutes, are you sure you want to start?[y/n]: ").strip().lower()
if start == "y":
print(math_quiz.run_quiz(set1))
print(math_quiz.show_results())
print(math_quiz.export_results())
elif start == "n":
print(":(")
else:
print("You can only choose between y/n!")
elif category == 2:
start = input(
"It can take from 2 to 10 minutes, are you sure you want to start?[y/n]: ").strip().lower()
if start == "y":
print(math_quiz.run_quiz(set2))
print(math_quiz.show_results())
print(math_quiz.export_results())
elif start == "n":
print(":(")
else:
print("You can only choose between y/n!")
elif category == 3:
print("See you :)!")
break
elif start == "n":
print(":(")
else:
raise ValueError("You can only choose between y/n!")
except ValueError as e:
print(f"Error occured: {e}")
if __name__ == "__main__":
main()