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
right_answers = 0
wrong_answers = 0
while len(questions) > 0:
question = random.choice(questions)
questions.remove(question)
print(question.text)
user_answer = input('Insert answer (Yes or No): ')
while user_answer not in ['yes', 'y', 'f', 'true', 'no', 'n', 'f', 'false']:
print('Sorry, that is an invalid answer')
user_answer = input('Please insert another answer (Yes or No): ')
if user_answer.lower() in question.correct_answer:
player_score += question.question_reward
right_answers += 1
print('You got it right! (+{} points)'.format(question.question_reward))
print('You have {} points'.format(player_score))
else:
wrong_answers += 1
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))
print('You answered {} questions right'.format(right_answers))
print('And {} questions wrong.'.format(wrong_answers))
game()
Quiz_Question.py
class Question():
def __init__(self, text, correct_answer, question_reward):
self.text = text
if correct_answer == 'True':
self.correct_answer = ['yes', 'y', 'f', 'true']
elif correct_answer == 'False':
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)]