-
Notifications
You must be signed in to change notification settings - Fork 1
/
Chapter 39 - QuizGame.py
78 lines (63 loc) · 1.88 KB
/
Chapter 39 - QuizGame.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
# CHAPTER 39
# QuizGame
# -------------------------------
def new_game():
guesses = []
correct_guesses = 0
question_num = 1
for key in questions:
print("-------------------------------")
print(key)
for i in options[question_num - 1]:
print(i)
guess = input("Enter (A, B, C, or D): ").upper()
guesses.append(guess)
correct_guesses += check_answer(questions.get(key), guess)
question_num += 1
display_score(correct_guesses, guesses)
# -------------------------------
def check_answer(answer, guess):
if answer == guess:
print("CORRECT")
return 1
else:
print("WRONG")
return 0
# -------------------------------
def display_score(correct_guesses, guesses):
print("\n--------------------------------")
print("RESULTS")
print("--------------------------------")
print("Answers: ", end="")
for i in questions:
print(questions.get(i), end=" ")
print()
print("Guesses: ", end="")
for i in guesses:
print(i, end=" ")
print()
print(f"Your score is: {int(correct_guesses/len(questions) * 100)}%")
# -------------------------------
def play_again():
print()
response = input("Do you want to play again? (yes/no)? ").lower()
if response == "yes":
return True
else:
return False
questions = {
"Who created Python? " : "A",
"What year was Python created? " : "B",
"Python is a tribute to which comedy group? " : "C",
"Is the Earth round? " : "A"
}
options = [
["A. Guido van Rossum", "B. Elon Musk", "C. Bill Gates", "D. Mark Zuckerberg"],
["A. 1989", "B. 1991", "C. 2000", "D. 2016"],
["A. Lonely Island", "B. Smosh", "C. Monty Python", "D. SNL"],
["A. True", "B. False", "C. Sometimes", "D. What is Earth?"]
]
new_game()
while play_again():
new_game()
print("Byeeeeee!")