-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Quiz.py
More file actions
55 lines (48 loc) · 1.71 KB
/
Copy pathPython Quiz.py
File metadata and controls
55 lines (48 loc) · 1.71 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
# Simple Python Quiz
def ask_question(question, options, correct_answer):
print(question)
for i, option in enumerate(options, 1):
print(f"{i}. {option}")
try:
answer = int(input("Your answer (1-4): "))
if options[answer - 1] == correct_answer:
return True
else:
return False
except (ValueError, IndexError):
print("Invalid input. Please select an option between 1 and 4.")
return False
def main():
print("Welcome to the Python Quiz!")
score = 0
questions = [
{
"question": "What is the capital of France?",
"options": ["Berlin", "Madrid", "Paris", "Rome"],
"correct_answer": "Paris"
},
{
"question": "Which planet is known as the Red Planet?",
"options": ["Earth", "Mars", "Jupiter", "Venus"],
"correct_answer": "Mars"
},
{
"question": "What is the largest ocean on Earth?",
"options": ["Atlantic", "Indian", "Arctic", "Pacific"],
"correct_answer": "Pacific"
},
{
"question": "Who wrote the play 'Romeo and Juliet'?",
"options": ["Charles Dickens", "Mark Twain", "William Shakespeare", "Jane Austen"],
"correct_answer": "William Shakespeare"
}
]
for q in questions:
if ask_question(q['question'], q['options'], q['correct_answer']):
print("Correct!\n")
score += 1
else:
print("Incorrect!\n")
print(f"You finished the quiz! Your score is {score}/{len(questions)}.")
if __name__ == "__main__":
main()