-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathround2.py
220 lines (167 loc) · 9.57 KB
/
round2.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
'''
Programmed by: Albert Chan
Programmed on: January 31, 2019
Programmed for: ICS3U1-04
Purpose: Create a function which prompts the user to enter the roots of a
randomly generated quadratic equation for Round 2 of the Math Quiz.
All randomly generated quadratic equations in this function will have
exactly 2 distinct real integer roots.
'''
# Modules are imported in order to invoke their functions
import random, math, rules
# Round #2 function
# Parameters are used to track the number of questions the user answered correctly and the total number of questions the user has answered
def r2(cor, tot):
'''
Parameter: The user's current score and the total number of problems asked so far are
passed as arguments to dynamically display the score whenever the
enters "S" or "s". The user will see their final score at the end.
Purpose: This function prompts the user to enter the roots of a randomly quadratic
equation (f(x) = ax^2 + bx + c) given, after this function is invoked.
The random quadratic equations given in this function will always have exactly
2 integer roots. This function determines the ability of the user to
find the roots of a quadratic equation. After each question the user will be
notified whether they answered the question correctly or not. This
function will also allow the user see an up-to-date score or rules if he/she
wants to.
Return: This function returns True if user answers correctly, and False otherwise.
'''
# Discriminant is originally defined as 0 (explained in next comment)
d = 0
'''
The discriminant of a quadratic equation cannot be less than 0
(if discriminant is than 0 there are no real roots); in this
function the discriminant cannot be equal to 0 (if the discriminant
is 0, there will be only be one distinct real root); this function
only asks the user to enter the roots of quadratic equations with
exactly 2 distinct real roots
'''
while d <= 0:
# The standard form of quadratic equation is f(x) = ax^2 + bx + c
# Coefficient a is between -100 and 100 and is randomly generated
a = random.randint(-100,100)
'''
Coefficient a in any quadratic equation cannot be equal to 0,
thus coefficient a is randomly generated until it is not 0
'''
while a == 0:
a = random.randint(-100,100)
# Coefficient b is between -100 and 100 and is randomly generated
b = random.randint(-100,100)
# Coefficient c is between -100 and 100 and is randomly generated
c = random.randint(-100,100)
# Calculation of the discriminant (explanation of what the discriminant can be is explained previously)
d = (b**2) - (4*a*c)
# 2 roots of the quadratic equation are calculated
rt1 = (-b-(math.sqrt(d)))/(2*a)
rt2 = (-b+(math.sqrt(d)))/(2*a)
'''
Determines whether the 2 roots of the quadratic equation are integers; the 2 roots must be integers in this program;
Coefficients are generated randomly until the 2 roots of the quadratic equation are integers
'''
while math.floor(rt1) != math.ceil(rt1) or math.floor(rt2) != math.ceil(rt2):
# Discriminant is originally defined as 0 (explained in next comment)
d = 0
'''
The discriminant of a quadratic equation cannot be less than 0
(if discriminant is than 0 there are no real roots); in this
function the discriminant cannot be equal to 0 (if the discriminant
is 0, there will be only be one distinct real root); this function
only asks the user to enter the roots of quadratic equations with
exactly 2 distinct real roots
'''
while d <= 0:
# The standard form of quadratic equation is f(x) = ax2 + bx + c
# Coefficient a is between -100 and 100 and is randomly generated
a = random.randint(-100,100)
'''
Coefficient a in any quadratic equation cannot be equal to 0,
thus coefficient a is randomly generated until it is not 0
'''
while a == 0 :
a = random.randint(-100,100)
# Coefficient b is between -100 and 100 and is randomly generated
b = random.randint(-100,100)
# Coefficient c is between -100 and 100 and is randomly generated
c = random.randint(-100,100)
# Calculation of the discriminant (explanation of what the discriminant can be is explained previously)
d = (b**2) - (4*a*c)
# 2 roots of the quadratic equation are calculated
rt1 = (-b-(math.sqrt(d)))/(2*a)
rt2 = (-b+(math.sqrt(d)))/(2*a)
# Formatting the output of the quadratic equation to the user based on the values of coefficients (all cases considered)
print("f(x) = ", end="")
print(("-" if a<0 else "")+(str(abs(a)) if abs(a)>1 else "")+"x^2 ", end="")
print(("- " if b<0 else "")+(("+ " if b>0 else "")+str(abs(b)) if abs(b)>1 else "")+("x " if b!=0 else ""), end="")
print(("- " if c<0 else "")+(("+ " if c>0 else "")+str(abs(c)) if abs(c)>1 else ""))
# User enters what they believe to be the two roots (separated by a comma) of the quadratic equation that is given (this is not a multiple choice quiz)
string = input("Enter the 2 roots separated by a comma: ")
# If the string the user enters has 1 comma and the length of the string is greater than 2 (used for error checking)
if string.count(",") == 1 and len(string) > 2:
stringSplit0 = string.split(",")[0]
stringSplit1 = string.split(",")[1]
# If the string after it is split has a length greater than 1 and has a negative sign (used for error checking)
if len(stringSplit0) > 1 and stringSplit0[0] == '-':
stringSplit0 = stringSplit0[1:]
if len(stringSplit1) > 1 and stringSplit1[0] == '-':
stringSplit1 = stringSplit1[1:]
# Checks the input of the user and outputs the corresponding output (error checking and checking the user's input)
while (string.count(",") != 1 or len(string) < 3 or (string.count(",") == 1 and not (stringSplit0.isdigit() and stringSplit1.isdigit()))):
# If the user inputs "R" or "r" the rules of the quiz are outputted to the user
if (string == "R" or string == "r"):
rules.rules()
# If the user inputs "S" or "s" the score of the quiz are outputted to the user
elif (string == "S" or string == "s"):
print("\n" + "-"*60)
print("Your current score is " + str(cor) + " out of " + str(tot) + ".")
print("You are currently on question #"+str((tot%5)+1)+" of round #"+str(math.ceil((tot+1)/5)))
print("-"*60,"\n")
# If the user inputs an invalid response, the user is prompted to enter what he/she thinks the are 2 roots
else:
print()
print("Your input is invalid! Please try again.")
print()
# Formatting the output of the quadratic equation to the user based on the values of coefficients (all cases considered; coefficients determined previously)
print("f(x) = ", end="")
print(("-" if a<0 else "")+(str(abs(a)) if abs(a)>1 else "")+"x^2 ", end="")
print(("- " if b<0 else "")+(("+ " if b>0 else "")+str(abs(b)) if abs(b)>1 else "")+("x " if b!=0 else ""), end="")
print(("- " if c<0 else "")+(("+ " if c>0 else "")+str(abs(c)) if abs(c)>1 else ""))
# User enters what they believe to be the two roots (separated by a comma) of the quadratic equation that is given (this is not a multiple choice quiz)
string = input("Enter the 2 roots separated by a comma: ")
# If the string the user enters has 1 comma and the length of the string is greater than 2 (used for error checking)
if string.count(",") == 1 and len(string) > 2:
stringSplit0 = string.split(",")[0]
stringSplit1 = string.split(",")[1]
# If the string after it is split has a length greater than 1 and has a negative sign (used for error checking)
if len(stringSplit0) > 1 and stringSplit0[0] == '-':
stringSplit0 = stringSplit0[1:]
if len(stringSplit1) > 1 and stringSplit1[0] == '-':
stringSplit1 = stringSplit1[1:]
ans1, ans2 = map(int, string.split(","))
# Determining whether the two roots the user enters are correct
# If the user enters both the correct roots
if ans1 == rt1 and ans2 == rt2:
print()
# Outputs if the user enters both the 2 correct roots
print("Congratulations! You are correct!")
print()
# Used to determine the score of the user
return True
# If the user enters both the correct roots
elif ans2 == rt1 and ans1 == rt2:
print()
# Outputs if the user enters both the 2 correct roots
print("Congratulations! You are correct!")
print()
# Used to determine the score of the user
return True
# If the user enters at least one wrong root
else:
print()
# Outputs if the user enters at least one wrong root
print("You are wrong!")
# Outputs the 2 correct roots
print("The 2 correct roots are:", str(int(round(rt1))) + " and " + str(int(round(rt2))))
print()
# Used to determine the score of the user
return False