5.4 - Quick quiz

Learning how one procedure can call another is an important step after writing simple, separate subprograms. This example uses a quiz game to show how a procedure can call another procedure.

Create a program that contains two procedures:

  • ask_question()
    • Stores two arrays: one with questions, the other with the matching answers.
    • Picks a random question using the same index in both arrays.
    • Displays the question, takes the user’s answer, and checks it.
    • If the answer is correct, display “Well done!”, otherwise display “Try again.”.
  • start_quiz()
    • Runs a loop that repeatedly calls ask_question() to ask one question each time.
    • After each call finishes, the player is asked if they want another question.
    • If the answer is “yes”, the loop continues; if not, the quiz ends with “Thanks for playing!”.

The main program should contain only a call to start_quiz().

questions = [
        "What is the capital of France?",
        "What is 5 + 3?",
        "What colour do you get if you mix red and blue?"
    ]
answers = ["Paris", "8", "Purple"]

 

Question: What is 5 + 3?
Your answer: 7
Try again.

Do you want another question? yes
Question: What is the capital of France?
Your answer: Paris
Well done!

Do you want another question? no
Thanks for playing!
PROCEDURE ask_question()
    SET questions TO ["What is the capital of France?",
                      "What is 5 + 3?",
                      "What colour do you get if you mix red and blue?"]

    SET answers TO ["Paris", "8", "Purple"]

    SET index TO random number between 0 and length(questions)-1

    OUTPUT "Question: " + questions[index]
    INPUT user_answer

    IF user_answer = answers[index] THEN
        OUTPUT "Well done!"
    ELSE
        OUTPUT "Try again."
    END IF
END PROCEDURE


PROCEDURE start_quiz()
    SET play_again TO "yes"
    WHILE play_again = "yes" DO
        CALL ask_question()
        OUTPUT "Do you want another question? "
        INPUT play_again
    END WHILE
    OUTPUT "Thanks for playing!"
END PROCEDURE


# main program
CALL start_quiz()

 

# main program
start_quiz()
import random

def ask_question():
    questions = [
        "What is the capital of France?",
        "What is 5 + 3?",
        "What colour do you get if you mix red and blue?"
    ]
    answers = ["Paris", "8", "Purple"]

    index = random.randint(0, len(questions) - 1)

    print("Question:", questions[index])
    user_answer = input("Your answer: ")

    if user_answer == answers[index]:
        print("Well done!")
    else:
        print("Try again.")
    print()

def start_quiz():
    play_again = "yes"
    while play_again == "yes":
        ask_question()
        play_again = input("Do you want another question? ")
    print("Thanks for playing!")

start_quiz()
import random

def ask_question():  # procedure that asks one random question
    # arrays storing questions and their matching answers
    questions = [
        "What is the capital of France?",
        "What is 5 + 3?",
        "What colour do you get if you mix red and blue?"
    ]
    answers = ["Paris", "8", "Purple"]

    # choose a random index to pick a question
    index = random.randint(0, len(questions) - 1)

    print("Question:", questions[index])
    user_answer = input("Your answer: ")

    if user_answer == answers[index]:
        print("Well done!")
    else:
        print("Try again.")
    print()  # blank line for spacing

def start_quiz():  # procedure that controls the game loop
    play_again = "yes"
    while play_again == "yes":
        ask_question()  # call to ask_question procedure
        play_again = input("Do you want another question? ")
    print("Thanks for playing!")

# main program 
start_quiz()  # call to start_quiz procedure

Extension

Update the program so that after a successful login, the show_agents() procedure asks the user which status they want to see (e.g. “active”, “retired”, “undercover”).
The program then displays all agents with that status.

Username: admin
Password: letmein
Login successful.

Which status do you want to see? retired
Agents with status 'retired':
Bourne