Python turtle and tkinter

Quiz Master

Make a quiz app with questions in a list, answer buttons, a score and a pop up box at the end.

Day 6 50 to 60 minutes 7 steps Key Stage 3
Start step 1

Today's app asks five questions, one at a time. Click an answer and it tells you straight away whether you were right, adds to your score, and moves on. At the end a pop up box gives your final score. Then you can swap in questions of your own and test your friends.

You will learn

  • Keep a whole quiz in a list of lists
  • Use one function to show any question
  • Check an answer and keep score
  • Switch buttons off and show a pop up message

You will need

  • A computer with Thonny or IDLE, or this page's Run it here button
  • Day 5's project, for labels, buttons and command

Words to know

list of lists
A list whose items are lists, like the rows of a table.
index
The position of an item in a list. The first item is number 0.
len
len(questions) is how many items a list has.
disabled
A button that is switched off: it is greyed out and cannot be clicked.

Step by step

  1. Step 1: A window with a question

    Save a new file as quiz.py. The question is a Label. wraplength=380 wraps long questions onto a new line instead of running off the side. The second import is for the pop up box at the end.

    quiz.pyPython
    import tkinter as tkfrom tkinter import messagebox window = tk.Tk()window.title("Quiz Master")window.geometry("420x330") question_label = tk.Label(window, text="Which of these is an input device?",                          font=("Arial", 14), wraplength=380)question_label.pack(pady=15) window.mainloop()
    The first question, at the top of the window.
  2. Step 2: Three answer buttons

    width=24 makes every button the same width, 24 letters wide. The buttons do nothing yet.

    New: lines 12 to 18.

    quiz.pyPython
    import tkinter as tkfrom tkinter import messagebox window = tk.Tk()window.title("Quiz Master")window.geometry("420x330") question_label = tk.Label(window, text="Which of these is an input device?",                          font=("Arial", 14), wraplength=380)question_label.pack(pady=15) button1 = tk.Button(window, text="Keyboard", width=24)button1.pack(pady=3)button2 = tk.Button(window, text="Monitor", width=24)button2.pack(pady=3)button3 = tk.Button(window, text="Speaker", width=24)button3.pack(pady=3) window.mainloop()
    Three answers under the question.
  3. Step 3: Put the questions in a list

    Each question is a small list of five things: the question, three answers and the right answer. questions is a list of those lists. show_question takes question number number and puts its parts on the label and buttons. q[0] is the question, q[1] to q[3] the answers.

    The widgets now start empty, and show_question() fills them in just before the window opens.

    New: lines 3 to 20, 26, 29, 31, 33 and 36.

    quiz.pyPython
    import tkinter as tkfrom tkinter import messagebox questions = [    ["Which of these is an input device?", "Keyboard", "Monitor", "Speaker", "Keyboard"],    ["What does CPU stand for?", "Central Processing Unit", "Computer Power Unit",     "Core Program Utility", "Central Processing Unit"],    ["How many bits are in a byte?", "4", "8", "16", "8"],    ["Which language is this quiz written in?", "Scratch", "HTML", "Python", "Python"],    ["What is the binary number 1010 in denary?", "10", "12", "1010", "10"],] number = 0 def show_question():    q = questions[number]    question_label.config(text=q[0])    button1.config(text=q[1])    button2.config(text=q[2])    button3.config(text=q[3]) window = tk.Tk()window.title("Quiz Master")window.geometry("420x330") question_label = tk.Label(window, text="", font=("Arial", 14), wraplength=380)question_label.pack(pady=15) button1 = tk.Button(window, text="", width=24)button1.pack(pady=3)button2 = tk.Button(window, text="", width=24)button2.pack(pady=3)button3 = tk.Button(window, text="", width=24)button3.pack(pady=3) show_question()window.mainloop()
    The same first question, now read from the list.
  4. Step 4: Check the answer

    check(choice) gets the number of the button that was clicked, 1, 2 or 3. If that answer matches the right answer, q[4], the feedback says Correct! in green, and otherwise it shows the right answer in dark red, firebrick. Then number goes up by 1 and the next question appears.

    A button's command cannot pass a number to a function, so each button has a tiny function of its own, choose1, choose2 or choose3, which calls check with its number.

    New: lines 22 to 41, 49, 51, 53 and 55 to 57.

    quiz.pyPython
    import tkinter as tkfrom tkinter import messagebox questions = [    ["Which of these is an input device?", "Keyboard", "Monitor", "Speaker", "Keyboard"],    ["What does CPU stand for?", "Central Processing Unit", "Computer Power Unit",     "Core Program Utility", "Central Processing Unit"],    ["How many bits are in a byte?", "4", "8", "16", "8"],    ["Which language is this quiz written in?", "Scratch", "HTML", "Python", "Python"],    ["What is the binary number 1010 in denary?", "10", "12", "1010", "10"],] number = 0 def show_question():    q = questions[number]    question_label.config(text=q[0])    button1.config(text=q[1])    button2.config(text=q[2])    button3.config(text=q[3]) def check(choice):    global number    right = questions[number][4]    if questions[number][choice] == right:        feedback.config(text="Correct!", fg="green")    else:        feedback.config(text="The answer was " + right, fg="firebrick")    number = number + 1    if number < len(questions):        show_question() def choose1():    check(1) def choose2():    check(2) def choose3():    check(3) window = tk.Tk()window.title("Quiz Master")window.geometry("420x330") question_label = tk.Label(window, text="", font=("Arial", 14), wraplength=380)question_label.pack(pady=15) button1 = tk.Button(window, text="", width=24, command=choose1)button1.pack(pady=3)button2 = tk.Button(window, text="", width=24, command=choose2)button2.pack(pady=3)button3 = tk.Button(window, text="", width=24, command=choose3)button3.pack(pady=3) feedback = tk.Label(window, text="", font=("Arial", 12))feedback.pack(pady=10) show_question()window.mainloop()
    Keyboard was right, and question 2 is up.
  5. Step 5: Keep score

    Add a score that goes up by 1 for each right answer, and a label to show it. show_question updates the score label every time.

    New: lines 14, 22, 25, 28 and 62 to 64.

    quiz.pyPython
    import tkinter as tkfrom tkinter import messagebox questions = [    ["Which of these is an input device?", "Keyboard", "Monitor", "Speaker", "Keyboard"],    ["What does CPU stand for?", "Central Processing Unit", "Computer Power Unit",     "Core Program Utility", "Central Processing Unit"],    ["How many bits are in a byte?", "4", "8", "16", "8"],    ["Which language is this quiz written in?", "Scratch", "HTML", "Python", "Python"],    ["What is the binary number 1010 in denary?", "10", "12", "1010", "10"],] number = 0score = 0 def show_question():    q = questions[number]    question_label.config(text=q[0])    button1.config(text=q[1])    button2.config(text=q[2])    button3.config(text=q[3])    score_label.config(text="Score: " + str(score)) def check(choice):    global number, score    right = questions[number][4]    if questions[number][choice] == right:        score = score + 1        feedback.config(text="Correct!", fg="green")    else:        feedback.config(text="The answer was " + right, fg="firebrick")    number = number + 1    if number < len(questions):        show_question() def choose1():    check(1) def choose2():    check(2) def choose3():    check(3) window = tk.Tk()window.title("Quiz Master")window.geometry("420x330") question_label = tk.Label(window, text="", font=("Arial", 14), wraplength=380)question_label.pack(pady=15) button1 = tk.Button(window, text="", width=24, command=choose1)button1.pack(pady=3)button2 = tk.Button(window, text="", width=24, command=choose2)button2.pack(pady=3)button3 = tk.Button(window, text="", width=24, command=choose3)button3.pack(pady=3) feedback = tk.Label(window, text="", font=("Arial", 12))feedback.pack(pady=10) score_label = tk.Label(window, text="Score: 0", font=("Arial", 12, "bold"))score_label.pack() show_question()window.mainloop()
    Two right and one wrong: the score is 2.
  6. Step 6: The end of the quiz

    After the last question, number is no longer less than len(questions), the number of questions, so check calls finish. It switches the buttons off with state="disabled" and opens a pop up box with the final score.

    New: lines 35, 36 and 46 to 53.

    quiz.pyPython
    import tkinter as tkfrom tkinter import messagebox questions = [    ["Which of these is an input device?", "Keyboard", "Monitor", "Speaker", "Keyboard"],    ["What does CPU stand for?", "Central Processing Unit", "Computer Power Unit",     "Core Program Utility", "Central Processing Unit"],    ["How many bits are in a byte?", "4", "8", "16", "8"],    ["Which language is this quiz written in?", "Scratch", "HTML", "Python", "Python"],    ["What is the binary number 1010 in denary?", "10", "12", "1010", "10"],] number = 0score = 0 def show_question():    q = questions[number]    question_label.config(text=q[0])    button1.config(text=q[1])    button2.config(text=q[2])    button3.config(text=q[3])    score_label.config(text="Score: " + str(score)) def check(choice):    global number, score    right = questions[number][4]    if questions[number][choice] == right:        score = score + 1        feedback.config(text="Correct!", fg="green")    else:        feedback.config(text="The answer was " + right, fg="firebrick")    number = number + 1    if number < len(questions):        show_question()    else:        finish() def choose1():    check(1) def choose2():    check(2) def choose3():    check(3) def finish():    score_label.config(text="Score: " + str(score))    question_label.config(text="Quiz over!")    button1.config(state="disabled")    button2.config(state="disabled")    button3.config(state="disabled")    messagebox.showinfo("Quiz Master", "You scored " + str(score) + " out of " + str(len(questions))) window = tk.Tk()window.title("Quiz Master")window.geometry("420x330") question_label = tk.Label(window, text="", font=("Arial", 14), wraplength=380)question_label.pack(pady=15) button1 = tk.Button(window, text="", width=24, command=choose1)button1.pack(pady=3)button2 = tk.Button(window, text="", width=24, command=choose2)button2.pack(pady=3)button3 = tk.Button(window, text="", width=24, command=choose3)button3.pack(pady=3) feedback = tk.Label(window, text="", font=("Arial", 12))feedback.pack(pady=10) score_label = tk.Label(window, text="Score: 0", font=("Arial", 12, "bold"))score_label.pack() show_question()window.mainloop()
    The quiz is over: a pop up box gives the score.
  7. Step 7: Add your own question

    Now make the quiz yours. Add a question to the list, in the same pattern: the question, three answers and the right answer, all in quotes. The quiz works out how many questions there are by itself.

    New: line 11.

    quiz.pyPython
    import tkinter as tkfrom tkinter import messagebox questions = [    ["Which of these is an input device?", "Keyboard", "Monitor", "Speaker", "Keyboard"],    ["What does CPU stand for?", "Central Processing Unit", "Computer Power Unit",     "Core Program Utility", "Central Processing Unit"],    ["How many bits are in a byte?", "4", "8", "16", "8"],    ["Which language is this quiz written in?", "Scratch", "HTML", "Python", "Python"],    ["What is the binary number 1010 in denary?", "10", "12", "1010", "10"],    ["Which of these is the fastest to get data from?", "RAM", "A hard disk", "A USB stick", "RAM"],] number = 0score = 0 def show_question():    q = questions[number]    question_label.config(text=q[0])    button1.config(text=q[1])    button2.config(text=q[2])    button3.config(text=q[3])    score_label.config(text="Score: " + str(score)) def check(choice):    global number, score    right = questions[number][4]    if questions[number][choice] == right:        score = score + 1        feedback.config(text="Correct!", fg="green")    else:        feedback.config(text="The answer was " + right, fg="firebrick")    number = number + 1    if number < len(questions):        show_question()    else:        finish() def choose1():    check(1) def choose2():    check(2) def choose3():    check(3) def finish():    score_label.config(text="Score: " + str(score))    question_label.config(text="Quiz over!")    button1.config(state="disabled")    button2.config(state="disabled")    button3.config(state="disabled")    messagebox.showinfo("Quiz Master", "You scored " + str(score) + " out of " + str(len(questions))) window = tk.Tk()window.title("Quiz Master")window.geometry("420x330") question_label = tk.Label(window, text="", font=("Arial", 14), wraplength=380)question_label.pack(pady=15) button1 = tk.Button(window, text="", width=24, command=choose1)button1.pack(pady=3)button2 = tk.Button(window, text="", width=24, command=choose2)button2.pack(pady=3)button3 = tk.Button(window, text="", width=24, command=choose3)button3.pack(pady=3) feedback = tk.Label(window, text="", font=("Arial", 12))feedback.pack(pady=10) score_label = tk.Label(window, text="Score: 0", font=("Arial", 12, "bold"))score_label.pack() show_question()window.mainloop()
    Six questions now, and the pop up box says out of 6.
    Watch out Every item ends with a comma, except the last in each list. A missing comma or quote is the most common bug today.

Test it

Tick each one when it works. If one does not, look back at the step it comes from.

Stretch challenges

Finished early? Try these. Each one has a hint if you get stuck.

  1. Shuffle

    Ask the questions in a different order every time.

    Show a hint

    import random at the top, then random.shuffle(questions) just before show_question() at the bottom.

  2. Play again

    Add a Play again button that starts the quiz from the first question with a score of 0.

    Show a hint

    Its function sets number and score back to 0 (both global), switches the answer buttons on again with state="normal" and calls show_question().

  3. Four answers

    Give every question four answers instead of three.

    Show a hint

    Every question needs one more answer, so the right answer moves to q[5]. Add a button4 with a choose4 function, and set its text in show_question.

The finished code

Show the whole program
quiz.pyPython
import tkinter as tkfrom tkinter import messagebox questions = [    ["Which of these is an input device?", "Keyboard", "Monitor", "Speaker", "Keyboard"],    ["What does CPU stand for?", "Central Processing Unit", "Computer Power Unit",     "Core Program Utility", "Central Processing Unit"],    ["How many bits are in a byte?", "4", "8", "16", "8"],    ["Which language is this quiz written in?", "Scratch", "HTML", "Python", "Python"],    ["What is the binary number 1010 in denary?", "10", "12", "1010", "10"],    ["Which of these is the fastest to get data from?", "RAM", "A hard disk", "A USB stick", "RAM"],] number = 0score = 0 def show_question():    q = questions[number]    question_label.config(text=q[0])    button1.config(text=q[1])    button2.config(text=q[2])    button3.config(text=q[3])    score_label.config(text="Score: " + str(score)) def check(choice):    global number, score    right = questions[number][4]    if questions[number][choice] == right:        score = score + 1        feedback.config(text="Correct!", fg="green")    else:        feedback.config(text="The answer was " + right, fg="firebrick")    number = number + 1    if number < len(questions):        show_question()    else:        finish() def choose1():    check(1) def choose2():    check(2) def choose3():    check(3) def finish():    score_label.config(text="Score: " + str(score))    question_label.config(text="Quiz over!")    button1.config(state="disabled")    button2.config(state="disabled")    button3.config(state="disabled")    messagebox.showinfo("Quiz Master", "You scored " + str(score) + " out of " + str(len(questions))) window = tk.Tk()window.title("Quiz Master")window.geometry("420x330") question_label = tk.Label(window, text="", font=("Arial", 14), wraplength=380)question_label.pack(pady=15) button1 = tk.Button(window, text="", width=24, command=choose1)button1.pack(pady=3)button2 = tk.Button(window, text="", width=24, command=choose2)button2.pack(pady=3)button3 = tk.Button(window, text="", width=24, command=choose3)button3.pack(pady=3) feedback = tk.Label(window, text="", font=("Arial", 12))feedback.pack(pady=10) score_label = tk.Label(window, text="Score: 0", font=("Arial", 12, "bold"))score_label.pack() show_question()window.mainloop()
Notes for the teacher

Timing

  1. 10 minutes: steps 1 and 2.
  2. 15 minutes: step 3. Lists of lists are new for most students: draw the list as a table on the board, one row a question.
  3. 20 minutes: steps 4 to 6.
  4. 10 minutes: step 7, then students swap seats and take each other's quizzes.

Mistakes to look out for

  1. A missing comma or quote inside the list: SyntaxError, often reported on the line after the real mistake.
  2. A right answer that does not exactly match one of the three answers, such as "keyboard" against "Keyboard": the question can never be answered correctly.
  3. global number, score missing from check: UnboundLocalError.
  4. check defined after the buttons that use it is fine, but choose1 and the others must be defined before the buttons are made, because command=choose1 needs the function to exist already.

Going further

The data (the questions) is kept apart from the code that uses it. Adding a question changes no code at all, which is how real apps are built. A good link to files: a stretch for keen students is to read the questions from a text file.