questions = [
{
'question': 'What is the capital of France?',
'options': ['Paris', 'London', 'Berlin', 'Madrid'],
'answer': 'Paris'
},
{
'question': 'Which planet is known as the Red Planet?',
'options': ['Earth', 'Mars', 'Venus', 'Jupiter'],
'answer': 'Mars'
}
]
def display_question(question):
print(question['question'])
for index, option in enumerate(question['options'], start=1):
print(f'{index}. {option}')
def collect_answer():
choice = input('Enter the number of your answer: ')
return int(choice) - 1
def run_quiz(questions):
score = 0
for question in questions:
display_question(question)
user_choice = collect_answer()
if question['options'][user_choice] == question['answer']:
score += 1
return score
if __name__ == '__main__':
print('Welcome to the Quiz!')
total_score = run_quiz(questions)
print(f'Your score is: {total_score}/{len(questions)}')
Welcome to the Quiz!
What is the capital of France?
1. Paris
2. London
3. Berlin
4. Madrid
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[1], line 34
32 if __name__ == '__main__':
33 print('Welcome to the Quiz!')
---> 34 total_score = run_quiz(questions)
35 print(f'Your score is: {total_score}/{len(questions)}')
Cell In[1], line 27, in run_quiz(questions)
25 for question in questions:
26 display_question(question)
---> 27 user_choice = collect_answer()
28 if question['options'][user_choice] == question['answer']:
29 score += 1
Cell In[1], line 21, in collect_answer()
19 def collect_answer():
20 choice = input('Enter the number of your answer: ')
---> 21 return int(choice) - 1
ValueError: invalid literal for int() with base 10: ''