forked from mosh-hamedani/python-projects-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz_game.py
More file actions
55 lines (43 loc) · 1.21 KB
/
quiz_game.py
File metadata and controls
55 lines (43 loc) · 1.21 KB
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
import random
from termcolor import cprint
QUESTION = 'question'
OPTIONS = 'options'
ANSWER = 'answer'
def ask_question(index, question, options):
print(f'Question {index}: {question}')
for option in options:
print(option)
return input('Your answer: ').upper().strip()
def run_quiz(quiz):
random.shuffle(quiz)
score = 0
for index, item in enumerate(quiz, 1):
answer = ask_question(index, item[QUESTION], item[OPTIONS])
if answer == item[ANSWER]:
cprint('Correct!', 'green')
score += 1
else:
cprint(f'Wrong! The correct answer is {item[ANSWER]}', 'red')
print()
print(f'Quiz over! Your final score is {score} out of {len(quiz)}')
def main():
quiz = [
{
QUESTION: 'What is the capital of France?',
OPTIONS: ['A. Berlin', 'B. Madrid', 'C. Paris', 'D. Rome'],
ANSWER: 'C'
},
{
QUESTION: 'Which planet is known as the red planet?',
OPTIONS: ['A. Earth', 'B. Mars', 'C. Jupiter', 'D. Saturn'],
ANSWER: 'B'
},
{
QUESTION: 'What is the largest ocean on Earth?',
OPTIONS: ['A. Atlantic', 'B. Indian', 'C. Arctic', 'D. Pacific'],
ANSWER: 'D'
}
]
run_quiz(quiz)
if __name__ == '__main__':
main()