-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPython quiz application.py
60 lines (46 loc) · 1.39 KB
/
Python quiz application.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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
# quiz.py
questions = [
{
"question": "What is the output of print(2 * 3 ** 3)",
"answers": ["18", "54", "6", "27"],
"correct_answer": "54"
},
{
"question": "Which of the following is not a Python data type?",
"answers": ["str", "int", "float", "tuple"],
"correct_answer": "tuple"
},
{
"question": "What is the output of print(abs(-5))",
"answers": ["5", "-5", "0", "None"],
"correct_answer": "5"
}
]
def ask_question(question):
print(question["question"])
for i, answer in enumerate(question["answers"], 1):
print(f"{i}. {answer}")
user_answer = input("Enter your answer (1-{}): ".format(len(question["answers"])))
return user_answer
def check_answer(question, user_answer):
if user_answer == "":
return False
if user_answer == question["correct_answer"]:
print("Correct!")
return True
else:
print("Incorrect. The correct answer is '{}'.".format(question["correct_answer"]))
return False
def run_quiz():
score = 0
for question in questions:
user_answer = ask_question(question)
if check_answer(question, user_answer):
score += 1
print("Your final score is: {}/{}".format(score, len(questions)))
if __name__ == "__main__":
run_quiz()
# In[ ]: