I want to share a programming activity with you which addresses several topics in the GCSE computer Science syllabus.
One of the challenges when following a syllabus with clearly delineated assessment objectives is that the knowledge required to become a competent programmer is deeply interconnected, and the various components rarely appear in isolation.
For example, the short program we are going to look at in this article is related to the following objectives from the Edexcel GCSE Computer Science Syllabus:
1.1.2 understand how to create an algorithm to solve a particular problem, making use of programming constructs (sequence, selection, iteration) and using appropriate conventions (flowchart, pseudocode, written description, draft program code)
1.1.5 Understand how to identify and correct errors in algorithms, including using trace tables
2.1.6 be able to determine what value a variable will hold at a given point in a program (trace table)
2.2.1 understand the structural components of a program (variable and type declarations, command sequences, selection, iteration, data structures, subprograms)
2.2.2 be able to use sequencing, selection and iteration constructs in their programs
2.4.1 understand how to write code that accepts and responds appropriately to user input
We are going to work our way up to writing a program which outputs a trace table for an algorithm which display a times table of the user’s choice. We will begin with some “warmup” exercises before attempting the full solution. Before looking at the solutions I have provided, have a go at the exercises using your preferred IDE (coding space).
Warmup Exercise 1: Write a Python program to print out all the values for the 5 times table up to
10 * 5.
# Five times table for i in range(1,11): print(5 * i)
Warmup Exercise 2: Now modify your program so the user can choose which times table to print.
# User inputs which times table they want to see table = int(input("Which times table do you want to see? ")) for i in range(1,11): print(i * table)
Remember we need to use
int to turn the input into a number
Warmup Exercise 3: Use tab characters
\t to print the following line containing column headings:
table index value OUTPUT
(Different editors may behave differently regarding their handling of tabs, so you may need to experiment with how many you use for your spacing.)
# Set up headings for columns, separated by tabs print("table\tindex\tvalue\tOUTPUT")
The main task: Put together all you have done so far and create a program that outputs the values for
table index value OUTPUT at every step in the iteration (for loop).
The result should look something like this:
table index value OUTPUT 6 1 6 6 6 2 12 12 6 3 18 18 6 4 24 24 6 5 30 30 6 6 36 36 6 7 42 42 6 8 48 48 6 9 54 54 6 10 60 60
Good luck and make sure you have a good go at it by yourself before looking at the solution below.
# Times Tables Trace Table # Get user input for which times table they want to see table = int(input("Which times table do you want to see? ")) print() # blank line for clearer display # Set up headings for columns, separated by tabs print("table\tindex\tvalue\tOUTPUT") # Count-controlled iteration - we know how many times we want to loop for index in range(1, 11): # We want the first 10 values (1 is included, 11 is not) value = index * table # Do the multiplication, store result in "value" # Output the results. We've used "+" and "str" to form out output. # There are several other ways we could have done this. print(str(table) + "\t" + str(index) + "\t" + str(value) + "\t" + str(value))
I hope that was a useful and fun activity – let me know how you got on in the comments below.