generated from fastai/fastpages
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquiz.py
96 lines (71 loc) · 2.51 KB
/
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
# @author Rohin Sood
from colorama import Fore
import sys
"""
declare all questions & answers within a dictionary
key: question
value: answer
"""
question_answers = {
"x\u00b2+3x+2": "(x+1)(x+2)",
"x\u00b2+x-6": "(x-2)(x+3)",
"a\u00b2-2a-35": "(a+5)(a-7)",
"3y\u00b2-15y+18": "3(y-2)(y-3)",
}
question_number = 0
correct_answers = 0
"""
print with the formatted colors & prefix
@param string - the string printed
"""
def format_print( string: str ):
print( Fore.BLUE + "\q\> " + Fore.WHITE + string + Fore.WHITE)
"""
format the input prompt with colors & prefix
@param string - the string printed
"""
def format_input ( string: str ):
return input( Fore.BLUE + "\q\> " + Fore.WHITE + string + Fore.WHITE)
"""
check if user answer is the same as the question answer
@param user_answer - the answer provided by the user
@param question - the key used to access the answer in questions_and_answers
"""
def check_answer( user_answer: str, question: str ):
if (user_answer == question_answers[question]):
return True
else:
return False
######
# Begin script!!
######
continue_quiz = format_input( Fore.GREEN + "Hello There!" + Fore.WHITE + " Are you ready to take this 4 question quiz on factoring polynomials? (y/n) ")
# exit the script if prompted
if continue_quiz != "y":
format_print( Fore.RED + "L goodbye")
sys.exit()
# print example question and response
format_print("Example Question #0: x\u00b2-16")
format_print("Example Answer: (x+4)(x-4)")
format_print( Fore.RED + "Remember not to add spaces!!" )
# wait for user to be fully ready
format_input("(Press enter to start the quiz) ")
format_print( Fore.GREEN + "Have Fun!!" )
# iterate through items in question_answers
for question, answer in question_answers.items():
# increase the question_number by one since indices
question_number += 1
# print the current question
format_print("~~ Question #" + str(question_number) + ": " + question)
# store the user answer
user_answer = format_input( Fore.YELLOW + "Your Answer: ")
if (check_answer(user_answer=user_answer, question=question)):
format_print( Fore.GREEN + "Correct!" + Fore.WHITE )
# increase correct answers by one
correct_answers += 1
else:
# print the correct answer if user is incorrect
format_print( Fore.RED + "Stupid ahh")
format_print("The correct answer is: " + Fore.GREEN + answer)
# print % of answers correct & leave
format_print( Fore.GREEN + "Quiz Complete! " + Fore.WHITE + "You got " + str(correct_answers/4*100) + r"% of answers correct :)")