How to make a true or false quiz in python

I have created the following program in Python:

def tf_quiz(question, correct_ans):
    if input(question) == correct_ans:
        return("correct")
    else:
        return("incorrect")

quiz_eval = tf_quiz("Birds can fly", "T")

print("your answer is", quiz_eval)

However, my output is as following:

Birds can flyF
your answer is incorrect

But I want the following output:

(T/F) Birds can fly: F
your answer is incorrect

What do I need to change in my code?

asked May 15, 2020 at 18:49

1

Your question is missing a "(T/F) " string at the beginning, and a colon and space at the end.

def tf_quiz(question, correct_ans):
    if input("(T/F) " + question + ": ") == correct_ans:
        return("correct")
    else:
        return("incorrect")

quiz_eval = tf_quiz("Birds can fly", "T")

print("your answer is", quiz_eval)

answered May 15, 2020 at 18:54

Either update the question text to have the exact formatting you want:

quiz_eval = tf_quiz("(T/F) Birds can fly: ", "T")

Or update the input() call to insert generic formatting for a true/false question:

if input("(T/F) %s: " % question) == correct_ans:

answered May 15, 2020 at 18:54

How to make a true or false quiz in python

John GordonJohn Gordon

24.6k7 gold badges29 silver badges51 bronze badges

Change:

if input(question) == correct_ans:

to:

if input(f"(T/F) {question}: ") == correct_ans

if you want the caller to be able to specify each question without the extra formatting pieces.

answered May 15, 2020 at 18:54

SamwiseSamwise

57.9k3 gold badges29 silver badges38 bronze badges

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)]

How do I create a quiz in Python?

Build a Quiz Application With Python.
Demo: Your Python Quiz Application..
Project Overview..
Prerequisites..
Step 1: Ask Questions. ... .
Step 2: Make Your Application User-Friendly. ... .
Step 3: Organize Your Code With Functions. ... .
Step 4: Separate Data Into Its Own File. ... .
Step 5: Expand Your Quiz Functionality..

How do you make a yes or no in Python?

how to make a yes or no question in python.
def question():.
i = 0..
while i < 2:.
answer = input("Question? ( yes or no)").
if any(answer. lower() == f for f in ["yes", 'y', '1', 'ye']):.
print("Yes").
break..
elif any(answer. lower() == f for f in ['no', 'n', '0']):.