1
1
from questions import quiz
2
2
3
+
3
4
def check_ans (question , ans , attempts , score ):
5
+ """
6
+ Takes the arguments, and confirms if the answer provided by user is correct.
7
+ Converts all answers to lower case to make sure the quiz is not case sensitive.
8
+ """
4
9
if quiz [question ]['answer' ].lower () == ans .lower ():
5
10
print (f"Correct Answer! \n Your score is { score + 1 } !" )
6
11
return True
7
12
else :
8
13
print (f"Wrong Answer :( \n You have { attempts - 1 } left! \n Try again..." )
9
14
return False
10
15
11
- score = 0
12
- for question in quiz :
13
- attempts = 3
14
- while attempts > 0 :
15
- print (quiz [question ]['question' ])
16
- answer = input ("Enter Answer: " )
17
- check = check_ans (question , answer , attempts , score )
18
- if check :
19
- score += 1
20
- break
21
- attempts -= 1
22
-
23
- print (f"Your final score is { score } ! \n Thanks for playing :D" )
16
+
17
+ def print_dictionary ():
18
+ for question_id , ques_answer in quiz .items ():
19
+ for key in ques_answer :
20
+ print (key + ':' , ques_answer [key ])
21
+
22
+
23
+ def intro_message ():
24
+ """
25
+ Introduces user to the quiz and rules, and takes an input from customer to start the quiz.
26
+ Returns true regardless of any key pressed.
27
+ """
28
+ print ("Welcome to this fun food quiz! \n Are you ready to test your knowledge about food?" )
29
+ print ("There are a total of 20 questions, you can skip a question anytime by typing 'skip'" )
30
+ input ("Press any key to start the fun ;) " )
31
+ return True
32
+
33
+
34
+ # python project.py
35
+ intro = intro_message ()
36
+ while True :
37
+ score = 0
38
+ for question in quiz :
39
+ attempts = 3
40
+ while attempts > 0 :
41
+ print (quiz [question ]['question' ])
42
+ answer = input ("Enter Answer (To move to the next question, type 'skip') : " )
43
+ if answer == "skip" :
44
+ break
45
+ check = check_ans (question , answer , attempts , score )
46
+ if check :
47
+ score += 1
48
+ break
49
+ attempts -= 1
50
+
51
+ break
52
+
53
+ print (f"Your final score is { score } !\n \n " )
54
+ print ("Want to know the correct answers? Please see them below! ;)\n " )
55
+ print_dictionary ()
56
+ print ("Thanks for playing! 💜" )
0 commit comments