# Beginner Quiz game (True or False)

This time I made a quiz game, in which the player is asked questions and receives points for each question answered correctly. Some people told me I should learn about classes, so this is what I came up with.

import random
from Quiz_Questions import game_questions

questions = [*game_questions]
def game():
print('-' * 20)
player_score = 0
while len(questions) > 0:
question = random.choice(questions)
questions.remove(question)
print(question.text)
while user_answer not in ['yes', 'y', 'f', 'true', 'no', 'n', 'f', 'false']:
print('Sorry, that is an invalid answer')
player_score += question.question_reward
print('You got it right! (+{} points)'.format(question.question_reward))
print('You have {} points'.format(player_score))
else:
print('You got it wrong, good luck next time!')
print('-' * 20)
print('Congratulations! You have answered all the questions!')
print('Your final score is {} points'.format(player_score))
game()


Quiz_Question.py

class Question():
self.text = text
self.correct_answer = ['yes', 'y', 'f', 'true']
self.correct_answer = ['no', 'n', 'f', 'false']
self.question_reward = question_reward
game_questions = [Question('Russia has a larger surface area than Pluto.', 'True', 7),
Question('There are as many stars in space than there are grains of sand on every beach in the world.', 'False', 5),
Question('For every human on Earth there are 1.6 million ants.', 'True', 8),
Question('On Jupiter and Saturn it rains diamonds.', 'True', 9),
Question('Scotland’s national animal is the phoenix.', 'False', 4),
Question('A banana is a berry.', 'True', 8),
Question('An octopus has three hearts.', 'True', 4),
Question('There are 10 times more bacteria in your body than actual body cells.', 'True', 9),
Question('Rhode Island is the closest US state to Africa.', 'False', 3),
Question('Shakespeare made up the name “Sarah” for his play Merchant of Venice.', 'False', 3)]


I would turn around the logic. The design you have has a lot of the general logic in the main program, and a lot of game-specific logic in the questions module. A better design for reusability would be to put the logic in a module, and the game-specific stuff (basically just the questions and perhaps some very superficial user interface) in the main program.

I would also define a separate class for the collection of questions; so maybe the main game might look like

from Quiz_Questions import QuestionsCollection, Question, PlayerState, QuestionsExhausted

collection = QuestionsCollection()

collection.add('Russia has a larger surface area than Pluto.', 'True', 7)
collection.add('There are as many stars in space than there are grains of sand on every beach in the world.', 'False', 5)
collection.add('For every human on Earth there are 1.6 million ants.', 'True', 8)
collection.add('On Jupiter and Saturn it rains diamonds.', 'True', 9)
collection.add('Scotland’s national animal is the phoenix.', 'False', 4)
collection.add('A banana is a berry.', 'True', 8),
collection.add('An octopus has three hearts.', 'True', 4)
collection.add('There are 10 times more bacteria in your body than actual body cells.', 'True', 9)
collection.add('Rhode Island is the closest US state to Africa.', 'False', 3)
collection.add('Shakespeare made up the name “Sarah” for his play Merchant of Venice.', 'False', 3)

def game():
print('-' * 20)
player = PlayerState()
questions = collection.game()
while True:
try:
question = questions.next()
except QuestionsExhausted:
break
print(question.challenge())
print(response)
print('-' * 20)
print('Congratulations! You have answered all the questions!')
print('Your final score is {} points'.format(player.score()))

game()


This is just a rough sketch, but hopefully hints at a different design where the classes encapsulate the behavior specific to each, with enough freedom to reassemble things in a different arrangement (multi-player game? Add timeouts?) and also with the user-facing I/O and message strings (mostly) separate from the library module.

This doesn't handle invalid input very elegantly but I'm thinking the way I started this, the Player class would be responsible for some I/O as well, so maybe actually refactor to

...
while True:
try:
question = questions.next()
except QuestionsExhausted:
break
player.handle(question)
print('Congratulations! You have answered all the questions!')
...

• Hey, sorry, I didn't understand much of what you did, but I'll keep that thing you said about separating the general logic and game-specific logic in mind. Thanks for your suggestions! Jan 11, 2020 at 20:46
if correct_answer == 'True':
self.correct_answer = ['yes', 'y', 'f', 'true']

f is recognized as a correct answer for both True and False answers, so I can collect all the points without knowing the correct answer to any of the questions by consistently answering f.
• Yeah, you are right, for True answers it should have been 't' instead of 'f', my mistake haha. Jan 26, 2020 at 19:56