Rainfall collector

Rainfall collector

Formatting rainfall data.

Try


Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.

Watch this video to learn about the new concepts shown in the program:

Knowledge Organiser

w = [[x for columns in range(y)] for rows in range(z)]

In this example, w is declared as a 2D array of y columns and z rows with each element assigned to be x.

x could be a number or a string. Typically to create an empty table, x would be the empty string "".

Although columns and rows are used as variables to make the code easy to read, this is an abstraction. How you visualise the table is up to you.

You could define a 2D list where the number of elements is unknown and can change, but you cannot refer to a specific index until it contains data.


Reading CSV files


Data can be stored in comma separated file (CSV) format, where each row (record) has data separated into columns (fields) by commas.


If the number of rows and columns is known, you can use a 2D array initialised with two for loops as shown above. If the number of rows is not known, a 2D list will be needed with new rows added to the list as they are read from the file.


This is a common operation when using files and 2D lists. Here is some boilerplate code that can be used to read data from a file and return it in a 2D list called database.


# Read data from filename and return a 2D list

def read_file(filename):

database = []

file = open(filename, "r")

# Read each line of data until end of file

for record in file:

record = record.strip()

fields = record.split(",")

database.append(fields)

file.close()

return database

Investigate


Questions to think about with this program to check your understanding:

Item question

Identify the line where a 2D array is initialised.

REVEAL ANSWER

Line 8:

data = [["" for columns in range(2)] for rows in range(num_regions)]

Structure question

Explain what the numbers 2 and 7 represent in the code in line 8.

REVEAL ANSWER

The number of rows and columns when the two-dimension structure is visualised as a table. Two columns and seven rows.

As the structure is zero-indexed, the first index is 0, so the indexes for seven rows are 0-6, and the columns 0-1.

Index
0
1
0

data[0][0]

data[0][1]

1

data[1][0]

data[1][1]

2

data[2][0]

data[2][1]

3

data[3][0]

data[3][1]

4

data[4][0]

data[4][1]

5

data[5][0]

data[5][1]

6

data[6][0]

data[6][1]

Make


Success Criteria

Change the program so that it:

  1. Includes a function called `short_label` that takes a parameter `label` and returns the first character from each word in label. E.g. South West would be SW. Hint: splitting the label into a list of words separated by a space and using a for loop to iterate the list extracting the first character is the easiest way to solve this problem.
  2. Includes a subprogram called, `output_short_table` that outputs the data in the format shown below.
  3. Calls the `output_short_table` subprogram after `output_table` in the main program.

Typical inputs and outputs from the program would be:

NW | 30

NE | 31

C | 13

E | 10

SE | 17

SW | 18

IoW | 16

Help

Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.

Evaluate


Compare the output from your program with the expected output below to check that your program has met the success criteria.

North West | 30

North East | 31

Central | 13

East | 10

South East | 17

South West | 18

Isle of Wight | 16


NW | 30

NE | 31

C | 13

E | 10

SE | 17

SW | 18

IoW | 16

Registered in England and Wales: 10442992

VAT Number: 290 9845 58

Telephone: 01452 947500

Email: admin@craigndave.co.uk

Craig'n'Dave logo

Craig 'n' Dave

Bett Awards 2024 Finalist