Consider showing the correct answer after the user has answered incorrectly.
Use your Google skills (or feel free to ask in the comments); techniques I have demonstrated in the code below that could benefit you:
ask_allbeing an iterator functionyielding 1 for every increase in score to be summed- PEP484 type hints
- immutable classes
- a
__main__guard
from string import ascii_uppercase
from typing import NamedTuple, Iterator
class Question(NamedTuple):
statement: str
options: tuple[str, ...]
correct: str
@property
def prompt(self) -> str:
return (
self.statement
+ '\nEnter'Enter '
+ ', '.join(
f'{letter} for {option}'
for letter, option in zip(ascii_uppercase, self.options)
)
+ ': '
)
def check(self, answer: str) -> bool:
return answer.lower() == self.correct
QUESTIONS = (
Question(
statement='Hitler party which came into power in 1933 is known as?',
options=('Labour Party', 'Nazi Party', 'Ku-Klux-Clan', 'Democratic Party'),
correct='b',
),
Question(
statement='In which year of First World War Germany declared war on Russia and France?',
options=('1914', '1915', '1916', '1917'),
correct='a',
),
Question(
statement='Joule is the SI unit of?',
options=('Temperature', 'Pressure', 'Energy', 'Current'),
correct='c',
),
Question(
statement='The famous wonder of the world is named, The Leaning Tower of _____',
options=('Siena', 'Pisa', 'Rome', 'Milan'),
correct='b',
),
Question(
statement='Which country is known as the "Land of Rising Sun"?',
options=('Japan', 'New Zealand', 'China', 'USA'),
correct='a',
),
Question(
statement='Which continent has the highest number of countries?',
options=('Asia', 'Europe', 'South America', 'Africa'),
correct='d',
),
Question(
statement='How many rings appear on the Olympics Flag?',
options=('1', '3', '5', '7'),
correct='c',
),
Question(
statement='Which Planet is the 3rd closest to the Sun?',
options=('Earth', 'Mars', 'Jupiter', 'Venus'),
correct='a',
),
Question(
statement='What is the largest planet in our Solar System?',
options=('Uranus', 'Saturn', 'Jupiter', 'Mercury'),
correct='c',
),
Question(
statement='What planet is nearest to The Earth?',
options=('Venus', 'Mercury', 'Mars', 'Pluto'),
correct='b',
),
Question(
statement='How many sides does a Heptagon have?',
options=('5', '6', '7', '8'),
correct='c',
),
)
def ask_all() -> Iterator[int]:
for i, question in enumerate(QUESTIONS, 1):
print(f'\nQ{i}: {question.statement}')
answer = input(f'Q{i}: {question.prompt}')
if question.check(answer):
print('Correct!')
yield 1
else:
print('Incorrectf'Incorrect! The answer was {question.correct.upper()}.')
yield 0
def main() -> None:
n = len(QUESTIONS)
print(
'Welcome to the Quiz Game!'
f'\nThere will be {n} questions. Good luck!'
)
score = sum(ask_all())
print(
'\nYou have successfully answered all the questions.'
"\nLet's see what your score is!"
f'\nYour score is {score} out of {n}.',
end=' '
)
if score >= n//2:
print(f'Well done!')
else:
print(f'You could do better!')
if __name__ == '__main__':
main()
Welcome to the Quiz Game!
There will be 11 questions. Good luck!
Q1: Hitler party which came into power in 1933 is known as?
Enter A for Labour Party, B for Nazi Party, C for Ku-Klux-Clan, D for Democratic Party: c
Incorrect! The answer was B.
Q2: In which year of First World War Germany declared war on Russia and France?
Enter A for 1914, B for 1915, C for 1916, D for 1917: c
Incorrect! The answer was A.
Q3: Joule is the SI unit of?
Enter A for Temperature, B for Pressure, C for Energy, D for Current: c
Correct!
Q4: The famous wonder of the world is named, The Leaning Tower of _____
Enter A for Siena, B for Pisa, C for Rome, D for Milan: c
Incorrect! The answer was B.
Q5: Which country is known as the "Land of Rising Sun"?
Enter A for Japan, B for New Zealand, C for China, D for USA: c
Incorrect! The answer was A.
Q6: Which continent has the highest number of countries?
Enter A for Asia, B for Europe, C for South America, D for Africa: c
Incorrect! The answer was D.
Q7: How many rings appear on the Olympics Flag?
Enter A for 1, B for 3, C for 5, D for 7: c
Correct!
Q8: Which Planet is the 3rd closest to the Sun?
Enter A for Earth, B for Mars, C for Jupiter, D for Venus: c
Incorrect! The answer was A.
Q9: What is the largest planet in our Solar System?
Enter A for Uranus, B for Saturn, C for Jupiter, D for Mercury: c
Correct!
Q10: What planet is nearest to The Earth?
Enter A for Venus, B for Mercury, C for Mars, D for Pluto: c
Incorrect! The answer was B.
Q11: How many sides does a Heptagon have?
Enter A for 5, B for 6, C for 7, D for 8: c
Correct!
You have successfully answered all the questions.
Let's see what your score is!
Your score is 4 out of 11. You could do better!