-
Notifications
You must be signed in to change notification settings - Fork 2
/
round3.py
166 lines (154 loc) · 6.55 KB
/
round3.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
import random, math, rules
def draw():
'''
Parameter: Not applicable for this function (no parameters)
Purpose: This function outputs a diagram of a sample triangle
to the user.
Return: No return value (returns None)
'''
print(" |\ ")
print(" |◡\ ")
print(" |b \ ")
print(" | \ C ")
print("A | \ ")
print(" | \ \tNote: Not drawn to scale")
print(" | \ ")
print(" |_ a\ ")
print(" |_|_____(\ ")
print(" B\n")
def r3(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 solve one other random variable to a given right
triangle (a side or an angle). The user will be given two random variables to a right triangle
(which can be two side lengths or one side length and one angle). This function determines the
ability of the user to do primary trigonometry. After each question the user will be
notified whether they answered the question correctly or not. This
function will allow the user see an up-to-date score or rules after each
question if he/she wants to.
Return: This function returns True if user answers correctly, and
False otherwise
'''
# List of possible given variables
var = ['A', 'B', 'C', 'a', 'b']
# One of the givens have to be a side
val1 = random.randint(11, 99)
var1 = var[random.randint(0,2)]
# Decides whether the user has to solve for a side or to solve for an edge
solveForSide = random.randint(0, 1)==0
# Initialize second given variable, second given value, and unknown to None
val2 = None
var2 = None
unknown = None
if (solveForSide):
# If user has to solve for a side, the user is given a side and an angle
var2 = var[random.randint(3, 4)]
val2 = random.randint(1, 89)
# Selects a random side that is not the same as the given side
unknown = var[random.randint(0, 2)]
while (unknown == var1):
unknown = var[random.randint(0, 2)]
else:
# If user has to solve for an angle, the user is given 2 sides
var2 = var[random.randint(0, 2)]
# Selects 2 distinct sides for given variables
while (var2 == var1):
var2 = var[random.randint(0, 2)]
# If one of the given sides is the hypotenuse, it must be the longest side
if (var2 == 'C'):
val2 = random.randint(val1+1, 100)
elif (var1 == 'C'):
val2 = random.randint(10, val1-1)
else:
val2 = random.randint(10, 100)
unknown = var[random.randint(3, 4)]
# Formats the given variables and the variable the user has to solve for
draw()
print("Given: \n"+str(var1)+" = "+str(val1)+" units\n"+str(var2)+" = "+str(val2)+(" degrees" if solveForSide else " units"))
print("Solve for "+str(unknown)+"\n")
userAns = input("Enter your answer rounded to the nearest integer: ")
# Keeps asking for input until user enters valid answer
while not userAns.isdigit():
if userAns == "R" or userAns == "r":
rules.rules()
elif userAns == "S" or userAns == "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")
else:
print("Your input is invaild! Please try again.\n")
draw()
print("Given: \n"+str(var1)+" = "+str(val1)+" units\n"+str(var2)+" = "+str(val2)+(" degrees" if solveForSide else " units"))
print("Solve for "+str(unknown))
userAns = input("Enter your answer rounded to the nearest integer: ")
userAns = int(userAns)
ans = None
# Solves for the unknown
if (solveForSide):
val2 = math.radians(val2)
if (var1 == 'A'):
if (var2 == 'a'):
if (unknown == 'B'):
ans = round(val1/math.tan(val2))
else:
ans = round(val1/math.sin(val2))
else:
if (unknown == 'B'):
ans = round(val1*math.tan(val2))
else:
ans = round(val1/math.cos(val2))
elif (var1 == 'B'):
if (var2 == 'a'):
if (unknown == 'A'):
ans = round(val1*math.tan(val2))
else:
ans = round(val1/math.cos(val2))
else:
if (var2 == 'b'):
if (unknown == 'A'):
ans = round(val1/math.tan(val2))
else:
ans = round(val1/math.sin(val2))
else:
if (var2 == 'a'):
if (unknown == 'A'):
ans = round(val1*math.sin(val2))
else:
ans = round(val1*math.cos(val2))
else:
if (unknown == 'A'):
ans = round(val1*math.cos(val2))
else:
ans = round(val1*math.sin(val2))
else:
if (var1 in 'AB' and var2 in 'AB'):
A = val1 if (var1 == 'A') else val2
B = val2 if (var1 == 'A') else val1
if (unknown == 'a'):
ans = round(math.degrees(math.atan(A/B)))
else:
ans = round(math.degrees(math.atan(B/A)))
elif (var1 in 'BC' and var2 in 'BC'):
B = val1 if (var1 == 'B') else val2
C = val2 if (var1 == 'B') else val1
if (unknown == 'a'):
ans = round(math.degrees(math.acos(B/C)))
else:
ans = round(math.degrees(math.asin(B/C)))
else:
A = val1 if (var1 == 'A') else val2
C = val2 if (var1 == 'A') else val1
if (unknown == 'a'):
ans = round(math.degrees(math.asin(A/C)))
else:
ans = round(math.degrees(math.acos(A/C)))
# Outputs if user is correct or not
if (userAns == ans):
print("Congratulations! You are correct!\n")
else:
print("You are wrong! The correct answer is "+str(ans)+(" units" if solveForSide else " degrees")+".\n")
return userAns == ans