Create a quiz in Python and test your friends.

Welcome to the seventh blog of Christmas!

I realize that Christmas was ten days ago and everybody’s gone back to work and school now, so that’s why these blogs aren’t very festive. I’m afraid that nobody has time during the Christmas season, and that’s why these blogs haven’t finished yet.

Anyway, here’s the song so far:

On the seventh day of Christmas my true love sent to me
seven quizzing questions
blog five improved
five objects tested
four lines of code
three LEDs in cardboard
two functions in a module
and a micro:bit in a Christmas tree

Open up your Python editor and type in the following code:

from random import randint
from time import sleep
questions = ["Who was Donald Trump's main competitor in the 2016 US presidental election? Was it\na: Barack Obama\nb:Hillary Clinton\nc:Spongebob Squarepants\n",
             "What is the square root of 169?\n",
             "Who is the current prime minister of the UK? Is it:\na:Boris Johnson\nb:David Cameron\nc:Theresa May\n",
             "Who wrote the James Bond books?Was it:\na:Ian Fleming\nb:J.K Rowling\nc:Usain Bolt\n",
             "What is the eleventh letter of the alphabet?\n",
             "What is x equal to in this equation:\na=4 b=6\nx=(a+b)*(a-3)\n",
             "How many countries are there in the world?\n"]
answers = ["b","13","c","a","k","10", "196"]
Correct = 0

Firstly we import the randint function from the random module and the sleep function from the time module. Then we  create a list with 7 questions in it, and a list with the answers in the same order. We also create a variable called Correct with a value of zero. This will count how many of the three questions are correct.

RanNum1 = randint (0,6)
RanNum2 = randint (0,6)
RanNum3 = randint (0,6)
if input (questions[RanNum1]) == answers[RanNum1]:
    Correct = Correct +1
if input (questions[RanNum2]) == answers[RanNum2]:
    Correct = Correct +1
if input (questions[RanNum3]) == answers[RanNum3]:
    Correct = Correct +1

Then we use the randint function to generate a random number from zero to six three times, and assign each random number to a variable. Next we ask the question that has the same index as the random number using the built-in input function, and if the player’s answer is equal to the answer in the answer list it will increase the Correct variable by one.

print ("Your score was:")
sleep (3)
print (Correct , "out of 3")
sleep (1)
if Correct == 3:
    print ("Well done!")
else:
    print ("Better luck next time")

Then we print out the player’s score (with a pause for tension) and either print “Well done!” or “Better luck next time” depending on whether the player’s had all the questions correct. Here’s the code all together:

from random import randint
from time import sleep
questions = ["Who was Donald Trump's main competitor in the 2016 US presidental election? Was it\na: Barack Obama\nb:Hillary Clinton\nc:Spongebob Squarepants\n",
             "What is the square root of 169?\n",
             "Who is the current prime minister of the UK? Is it:\na:Boris Johnson\nb:David Cameron\nc:Theresa May\n",
             "Who wrote the James Bond books?Was it:\na:Ian Fleming\nb:J.K Rowling\nc:Usain Bolt\n",
             "What is the eleventh letter of the alphabet?\n",
             "What is x equal to in this equation:\na=4 b=6\nx=(a+b)*(a-3)\n",
             "How many countries are there in the world?\n"]
answers = ["b","13","c","a","k","10", "196"]
RanNum1 = randint (0,6)
RanNum2 = randint (0,6)
RanNum3 = randint (0,6)
if input (questions[RanNum1]) == answers[RanNum1]:
    Correct = "1"
if input (questions[RanNum2]) == answers[RanNum2]:
    Correct = "2"
if input (questions[RanNum3]) == answers[RanNum3]:
    Correct = "3"
print ("Your score was:")
sleep (3)
print (Correct , "out of 3")
sleep (1)
if Correct == 3:
    print ("Well done!")
else:
    print ("Better luck next time")

Our code’s advantage is that it asks three random questions which make it more interesting to play multiple times, but it’s disadvantage is that scores can’t really be compared as one person might have had three easy question and another might have had three hard questions.

Challenge:
Can you keep track of which questions the player’s got wrong?

Challenge 2:
Can you stop the same question from being asked twice? Maybe you could ensure that each random number is different.

Advertisements