Something that computer programs are really good at is maths. Something that children often struggle with is maths. Let’s write a number of small programs to help students practice their maths. We will start with a program that tests the user on their times' tables. Then we will look at practising sums before looking at other more complicated types of mathematics. The type of maths that you will have done in years 7 and 8 at school. When writing computer programs we often use exactly the same types of maths that we do in a maths lesson. One small difference is that the equals sign is on the other side. In maths we have:
a X b = c
In programming we have:
c = a X b
In this case, we set the value of c to be the result of the algebra equation a X b. In programming, the variable that we want to change is always on the left of the equals sign.
As we write these programs we will be using the three fundamental constructs of computer programming; Sequences of instructions, iterations or loops to repeat instructions, and decisions to do different things depending on whether or not a condition is met.
In the second program, we will make things more complicated by asking questions in a random order.
We will start with a very simple program that lets the user enter a number for the times' table that they want to practise and then loop around 12 times asking the user to enter the answer to the sum that is in the form of a X b = c where a is the times' table that the user wants to practice, b is the next number in the sequence 1,2,3…12 and c is the answer the user types in. We then need to check to see if the user's answer is correct. If so, we update their score, if not we don’t update their score but we tell them the correct answer.
| Step | Functionality |
|---|---|
| 1 | Print instructions and a friendly message to encourage the student to practise their maths and then ask the user their name. Then ask what times’ table they want to practise |
| 2 | Start the loop that loops exactly 12 times |
| 3 | Store the correct answer in a variable and then ask the user to input their answer |
| 4 | If the user's answer is the same as the value in the variable holding the correct answer, update their score by one. If not, then tell them the correct answer |
| 5 | Once the loop has finished, print out the final score |
Use the print command to print out a friendly greeting and the input command to ask the user questions. When asking for a number, the answer they type in needs to be turned into a number. This is because “12” is not the same as 12. I know it sounds weird but computers handle numbers and letters in different ways. Sometimes “12” is treated like letters and you can’t do maths on letters (or at least not the type of maths that we want to do here). This is why you will sometime see the input command with int() around it. int() stands for integer or whole number. This changes the letters into numbers. At the start of this program, we will use input without turning it into a number when asking the user for their name and with the int() command when asking for the times' table that they want to practise. I will leave it up to you to print the instructions that you want to give.
print("instructions")
name = input("Please enter your name ")
print("Hello", name)
a = int(input("What times table do you want to practise "))
score = 0
Test your code. Make sure it asks the questions and prints out as you expect
If we know exactly how many times we want to repeat some instructions we can use a for loop. In python, this will loop and number the loop indicator (the number of the loop) from 0 to the number indicated minus -1. I know, another weird thing but if you think about it it makes sense. If I want to loop around 12 times but start from 0, the last time around the loop, the indicator will be 11. The loop indicator will be 0,1,2,3,4,5,6,7,8,9,10,11. This gives 12 iterations starting from 0. For us to practise times tables that go from 1 to 12 rather than 0 to 11 we need to ask the loop to go to 13 (one more than the highest number we want).
for b in range(1,13):
As we go around this loop 12 times (from 1 to 13-1) the variable a will contain the indicator. This is what we will use to build the sum that we will ask the user to solve.
If you test your program now, you will find that it does not work yet. This is because the for loop is expecting to repeat some instructions but we haven't yet given it any.< Let’s do that now
In the design, we said we wanted to build the sum so that it looks like a X b = c. We now have a and b and we will ask the user to enter c. Before we do that, the program needs to know the correct answer first. When we have both the user's answer and the correct answer, we can compare the two and tell if the user has got the answer correct or not. When writing instructions that need to be repeated in the loop, those instructions need to be indented (moved over to the right. This is how python knows what to repeat and what not to repeat.
for b in range(1,13):
correctAnswer = a * b
print("What is ",a," * ",b,"?")
c = int(input())
We now have a, b and c as well as the correct answer. If the user's answer(in the variable C) is the same as the correct answer, we can increase the score. If not, we must tell the user the correct answer. The program, therefore, needs to make a decision based on whether or not the user got the sum right. The if statement is used to check this.
print("instructions")
name = input("Please enter you name ")
print("hello",name)
a = int(input("What times table do you want to practise "))
score = 0
for b in range(1,13):
correctAnswer = a * b
print("What is ",a," * ",b,"?")
c = int(input())
if c == correctAnswer:
print("Correct")
score = score +1
print("Your score is now",score)
else:
print("Incorrect")
print("The correct answer is",correctAnswer)
Test your program. Is it getting the answers right when you enter the wrong answer? Is the score increasing only when you enter the correct answer?
Now the program is almost complete. The last thing to do is to print out the final result. Make sure that this last instruction is not included in either the if statement or the for loop by placing the code as far left as possible, without any indentation.
for b in range(1,13):
correctAnswer = a * b
print("What is ",a," * ",b,"?")
c = int(input())
if c == correctAnswer:
print("Correct")
score = score +1
print("Your score is now",score)
else:
print("Incorrect")
print("The correct answer is",correctAnswer)
print("Well Done. Your total score was ",score," out of 12")
Test your program
Add the user name into the final “well done” line
At the start of the program, ask the user the maximum number that they want to pracise up to. This way they won’t be restricted to 12. Store this number in a variable called max and replace the 12 in the for loop with this variable (don’t forget to use max+1)
Python is a very powerful language that uses lots of different libraries to do different things. The first library that we will use is the random library
We will use most of the code we have already written but add some instructions to:
This is the same as before but we will import the random library. Once a library has been imported once, it can be used many times. If all of the import commands (sometime you will want to import many different libraries) are put at the very start of the program, they can be used anywhere else in the program.
import random
print("different instructions")
name = input("Please enter your name ")
print("hello", name)
a = int(input("What times table do you want to practise "))
score = 0
Now test your program. It should behave exactly the same as before
The for loop is exactly the same as before but instead of using the loop indicator in the sum, we will bet a random integer between 1 and 12 (or between 1 and max if you have made that change). To stop confusion, we will change the loop indicator variable to “x” so that we can put the multiplier in to the variable b as we did above.
import random
print("different instructions")
name = input("Please enter you name ")
print("hello",name)
a = int(input("What times table do you want to practise "))
score = 0
for x in range(1,13):
b = random.randint(1,12)
correctAnswer = a * b
print("What is ",a," * ",b,"?")
c = int(input())
if c == correctAnswer:
print("Correct")
score = score +1
print("Your score is now",score)
else:
print("Incorrect")
print("The correct answer is",correctAnswer)
print("Well Done. Your total score was ",score," out of 12")
Test your program. You may notice that you get the same sum more than once and other sums that you would expect, never come up. This is because the random number does not keep a track of what it has already used.
Now we need to create a list and add each number that we have got to the list. We can then check if we have had the number before. If so, we can get another number. To use this we need to set up some more variables at the start of the program. This will be the list with the first value that we DON’T want to ue and we need to initialize (set the first value of) the variable b to be the same as the value we don’t want to use.
We now need a loop to get our random number. This will be a different type of loop. Because we don’t know how many times we have to try to get a new number before we get one we have not had before, we can’t use a for loop. The new loop is a while loop (make sure that the instruction to get the value of b is indented because this is the instruction that we need to repeat). When we found a number that we have not used before, we can add this to the end of the list using listName.append() (listName will be replaced with the name of our list)
import random
print("different instructions")
name = input("Please enter you name ")
print("hello",name)
a = int(input("What times table do you want to practise "))
score = 0
usedList = [0]
b=0
for x in range(1,13):
while b in usedList:
b = random.randint(1,12)
usedList.append(b)
correctAnswer = a * b
print("What is ",a," * ",b,"?")
c = int(input())
if c == correctAnswer:
print("Correct")
score = score +1
print("Your score is now",score)
else:
print("Incorrect")
print("The correct answer is",correctAnswer)
print("Well Done. Your total score was ",score," out of 12")
Now test your program. This time, make sure that you get each value only once.