Maths and Raspberry Pi – Times Tables using Python

Posted: January 13, 2014 by schoolpiclub in General News

Today we looked at a Times Tables quiz using Python.

The quiz simply asks 10 random Times Tables questions and tells the students if they have got it right or wrong as they go along. At the end it gives them their score out of 10.

image_7

Here is the code (please note use IDLE and not IDLE3):-

from random import randint
correct = 0
for i in range(10):
    n1 = randint(1, 10)
    n2 = randint(1, 10)
    prod = n1 * n2
    ans = input(“What’s %d times %d? ” % (n1, n2))
    if ans == prod:
        print “That’s right — well done.\n”
        correct = correct + 1
    else:
        print “No, I’m afraid the answer is %d.\n” % prod
print “\nI asked you 10 questions.  You got %d of them right.” % correct
print “Well done!”
image_1

We then looked at how we could modify the code, for example, what if a student just wanted to learn their 8 times tables. So TW changed the fifth line of code to n2 = 8 the quiz then just asked 8 times tables questions – which he got every single one right! We then looked at other things we could modify. We changed the code to ask addition questions between 1-50, and we also increased the number of questions asked.

image_4

We are hoping to further develop the quiz by personalising it to say the students name or randomly pick a student to answer the question so that the quiz can be used in Maths lessons.

So a Raspberry Pi is not just about learning computing and coding it can help improve your numeracy skills. Maybe a literacy challenge should be next!

Advertisements

Comments are closed.