Skip to content

Quiz time

Make a quiz that checks each answer, keeps your score, and tells you how well you did.

Start here: open pygwide. On the home page, choose Tutorials, then Python Basics, then Lesson 3: Quiz time.

This lesson takes about an hour. The extensions take about 30 minutes more.

What you will learn

  • How to make decisions with if and else.
  • How to check if two things are the same with ==.
  • How indenting tells Python which lines belong to an if.
  • How to keep a score that goes up.
  • How to choose between more than two things with elif.

What you need from before

  • Variables and input(), from Lesson 2.
  • f-strings, from Lesson 2.

Step 1: Your first question

Type this and run it. Try a right answer, then run it again and try a wrong one.

answer = input("What is the capital of France? ")
if answer == "Paris":
    print("Correct!")
else:
    print("Sorry, it is Paris.")

if checks whether something is true. If it is, Python runs the lines underneath. If not, it runs the lines under else.

Look carefully:

  • The if and else lines end with a colon :.
  • The lines underneath are pushed in, or indented. pygwide indents the next line for you after a colon. Indenting is how Python knows which lines belong to the if.

In Scratch

This is like the if then else block. The indented lines are the blocks inside it.

Step 2: One equals or two?

  • One = means "remember this as", as in score = 0.
  • Two == means "is this the same as?", as in answer == "Paris".

The answer must match exactly. If you type paris with a small p, it is not the same as "Paris". You'll fix that in an extension.

In Scratch

== is like the green = block from Operators.

Step 3: Keep score

Make a variable called score at the top, and add 1 to it for each right answer:

score = 0

answer = input("What is the capital of France? ")
if answer == "Paris":
    print("Correct!")
    score = score + 1
else:
    print("Sorry, it is Paris.")

print(f"You scored {score}.")

score = score + 1 means "work out score + 1, then remember that as score".

In Scratch

score = score + 1 is like change score by 1.

Step 4: More questions

Add two more questions, each before the last print() line. For a number answer, compare with a string, such as "8", because input() gives you a string:

answer = input("How many legs does a spider have? ")
if answer == "8":
    print("Correct!")
    score = score + 1
else:
    print("Sorry, a spider has 8 legs.")

Then write a third question of your own.

Step 5: How did you do?

At the end, give a different message for each score. elif means "else, if". Python checks each one in turn, and runs only the first that is true:

score = 3
print(f"You scored {score} out of 3.")
if score == 3:
    print("A perfect score! Well done!")
elif score == 2:
    print("Great job!")
else:
    print("Good try! Play again to beat your score.")

Here are more ways to compare things:

Python Means
== is the same as
!= is not the same as
< is less than
> is more than
<= is less than or the same as
>= is more than or the same as

Check your code

# My quiz

score = 0
print("Welcome to my quiz!")
print()

answer = input("What is the capital of France? ")
if answer == "Paris":
    print("Correct!")
    score = score + 1
else:
    print("Sorry, it is Paris.")

answer = input("How many legs does a spider have? ")
if answer == "8":
    print("Correct!")
    score = score + 1
else:
    print("Sorry, a spider has 8 legs.")

answer = input("What is 7 times 6? ")
if answer == "42":
    print("Correct!")
    score = score + 1
else:
    print("Sorry, it is 42.")

print()
print(f"You scored {score} out of 3.")
if score == 3:
    print("A perfect score! Well done!")
elif score == 2:
    print("Great job!")
else:
    print("Good try! Play again to beat your score.")

Extensions

Any capital letters

.lower() turns a string into small letters, so "PaRiS".lower() is "paris". Use it to accept an answer however it is typed:

if answer.lower() == "paris":
    print("Correct!")
One way to do it
# My quiz, which accepts any capital letters

score = 0
print("Welcome to my quiz!")
print()

answer = input("What is the capital of France? ")
if answer.lower() == "paris":
    print("Correct!")
    score = score + 1
else:
    print("Sorry, it is Paris.")

answer = input("What colour do you get if you mix blue and yellow? ")
if answer.lower() == "green":
    print("Correct!")
    score = score + 1
else:
    print("Sorry, it is green.")

print()
print(f"You scored {score} out of 2.")

Make it harder

Add more questions, and change the messages at the end to match. Can you ask a question whose answer is a sum, using int() from Lesson 2?

Extra bits

Fix the bugs

These two programs each have a mistake. Run them and read the errors.

answer = input("What is 2 + 2? ")
if answer == "4"
    print("Correct!")
answer = input("What is 2 + 2? ")
if answer == "4":
print("Correct!")

Export your project

pygwide keeps your work in this web browser, but club computers can forget it. Press ⇩ Export to save your project as a zip file. Keep it somewhere safe, such as a USB stick or your school drive.

Next time, choose ⇪ Import a project on the home page to carry on with it.