Skip to main content
added 277 characters in body
Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257

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_all being an iterator function yielding 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!
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 '
            + ', '.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()
        answer = input(f'Q{i}: {question.prompt}')
        if question.check(answer):
            print('Correct!')
            yield 1
        else:
            print('Incorrect!')
            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!

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!

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!

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!

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!

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!

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!

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!

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_all being an iterator function yielding 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 (
            '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(question.prompt)
        if question.check(answer):
            print('Correct!')
            yield 1
        else:
            print(f'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!
Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257

How is my progress so far (~8-10 days of learning)?

Progress is non-linear, and technical learning is a long, long road (years-scale, not days-scale). So it's impossible to assess like this, but "keep it up".

How would you rate it in terms of efficiency?

That's not the right question to ask: run-time or memory efficiency are irrelevant for a program like this. Instead work on maintainability, legibility and correctness.

Otherwise:

sleep() in a console program is a pet peeve of mine and offers nothing to the user. I would drop these.

Avoid globals - score certainly doesn't need to be one.

Consider using a very simple NamedTuple or dataclass to represent each question uniformly.

Do not bake in your Qx: to your prompt, nor your A for, B for. These should all be generated. Among other things this will fix an error of a repeated Q3 and missing Q: prefixes from your last few questions.

Do not hard-code 10; you in fact have 11 questions.

Typo in Invlaid.

Not a review of code, but "What planet is nearest to The Earth?" is a meaningless question to ask since the distance always changes depending on true anomaly at the time. You could instead ask Which planet's orbit is nearest to that of Earth?

Suggested

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 '
            + ', '.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()
        answer = input(f'Q{i}: {question.prompt}')
        if question.check(answer):
            print('Correct!')
            yield 1
        else:
            print('Incorrect!')
            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()

Output

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!

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!

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!

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!

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!

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!

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!

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!