-
Notifications
You must be signed in to change notification settings - Fork 3
/
math_quiz.py
159 lines (121 loc) · 4.44 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from datetime import datetime, date, time, timedelta
from prettytable import PrettyTable
import os
import random
DIFFICULTY_OPTIONS = {
1: "EASY",
2: "MEDIUM",
3: "HARD"
}
# Used for linux
sfile = "/tmp/highscores.txt"
# Used for Windows
#sfile = "C:\\temp\\highscores.txt"
def get_highscores():
# This method handles grabbing the current high scores before each game
# Creates a prettytable object for sorting and displaying highscores
table = PrettyTable()
table.field_names = ["Name","Difficulty","Correct","Total","Duration"]
# If the highscore file doesn't exist, create it
if os.path.exists(sfile):
f = open(sfile, "r")
else:
f = open(sfile,"w+")
print("\n")
# Grab the current highscores if any
contents = f.read()
for line in contents.splitlines():
table.add_row([line.split(",")[0],line.split(",")[1],line.split(",")[2],line.split(",")[3],line.split(",")[4]])
f.close()
table.sortby = "Duration"
table.reversesort = False
print(table.get_string(title="Highscores",start=0,end=9))
print("\n\n")
def update_highscores(player, difficulty, correct_answers, number_of_questions, duration):
""" This method handles updating the high score of each player """
# Grab the total seconds for the duration and format it to 2 decimal places
total_seconds = duration.total_seconds()
total_seconds_dec = float("%.2f" % total_seconds)
# Grab the english corresponding value for difficulty
difficulty = DIFFICULTY_OPTIONS[difficulty]
# Open the highscore file for appending so we can add our new values
target = open(sfile,'a')
target.write("{},{},{},{},{}".format(player, difficulty, correct_answers, number_of_questions, total_seconds_dec))
target.write("\n")
target.close()
def get_format(anumber):
chkformat = random.randint(1,4)
if chkformat == 1:
#decimal
newnbr = anumber
fmtstring = 'Decimal'
elif chkformat == 2:
#hex
newnbr = hex(anumber)
fmtstring = 'Hex'
else:
#octal
newnbr = oct(anumber)
fmtstring = 'Octal'
return newnbr, fmtstring
def get_mathfunc():
mthfunc = random.randint(1,3)
if mthfunc ==1:
return "ADD"
else:
return "SUBTRACT"
def play_game():
# Show the highscores
get_highscores()
# Grab the begin time of the game
start_time = datetime.now()
# Grab the player name to keep track of highscores
player = input("Name of player: ")
player = str(player)
# Grab the number of questions to ask
number_of_questions = input("How many questions?: ")
number_of_questions = int(number_of_questions)
# Grab the difficulty of the questions
difficulty = input("Press 1 for easy, 2 for medium, and 3 for hard:")
difficulty = int(difficulty)
# Placeholder for number of correct answers
correct_answers = 0
# Ask N # of questions
for nbr1 in range(number_of_questions):
if difficulty == 1:
nbr1 = random.randint(1,33)
nbr2 = random.randint(1,33)
elif difficulty == 2:
nbr1 = random.randint(1,101)
nbr2 = random.randint(1,101)
else:
nbr1 = random.randint(1,1001)
nbr2 = random.randint(1,1001)
domath = get_mathfunc()
if domath == "ADD":
answer = nbr1 + nbr2
else:
answer = nbr1 - nbr2
nbr1,nbr1fmat = get_format(nbr1)
nbr2,nbr2fmat = get_format(nbr2)
question = nbr1fmat + " " + str(nbr1)
question = question + " " + domath + " "
question = question + nbr2fmat + " " + str(nbr2) + " = "
print('\n')
print(question)
myanswer = input("What is the answer to the question in decimal? ")
myanswer = int(myanswer)
if myanswer == answer:
print("Correct\n\n")
correct_answers += 1
else:
print("Wrong!!!")
print("Your answer was {} and the correct answer is {}.\n \n".format(myanswer, answer))
# Game has finished, so grab the end time
end_time = datetime.now()
# Get the duration it took
date_diff = end_time - start_time
print("You completed the game in {} with {} of {} correct".format(date_diff, correct_answers, number_of_questions))
# Update the highscores
update_highscores(player, difficulty, correct_answers, number_of_questions, date_diff)
play_game()