Skip to content

Workshop 2 - Sem 1 2024

Note

  • Make sure you have downloaded assignment_1_config.py and assignment_1_data.py. These two files need to be in the same file as your assignment.
  • Do not make any changes to these two files.
  • Keep uploading copies to Canvas as you work on your code. Don't worry about it being complete.You want to make sure that you have a somewhat working code than nothing at all!
  • Don't worry too much about how long your code is. The priority is that it works and it is understandable.

Warning

This is just to get you started with the assessment. Please review the video or transcript to ensure you meet all the criteria.

Extracting the Raw Data

First, you need to go to the visualise_data function. In the default file given to you, it will look like this.

def visualise_data(rename_me_in_task_1b):

You need to rename this parameter. The new name you choose for the parameter should be the name you use in the loop to call it there.

From here, you should be able figure out what the data looks like by doing a print statement like so:

print(data)

It will give you a random seed each time. In your terminal, you should see something like this:

Using seed 38756
(Argument to function data_set) ...

The monthly reports to be visualised are:

[['March', 2],
 ['November', 1],
 ['December', 3],
 ['February', 1],
 ['June', 0],
 ['August', 1],
 ['September', -1]]

Tip

Print statements will be your best friend when debugging code

Processing the Data

  1. Now that you know how the data looks like once its called into your code you need. You need to separate the two parts of the array. You can do this by doing something similar to the code below.
  2. Since the month goes first in the array, you can go ahead and process that first. This first set of if statements should make sure that the program is moving in a chronological order throughout the program. You should think about what the x coordinates should be.
  3. Next we need to process the number of times the image is drawn into the program. You can do this by writing another set of if statements right below the first set. This is also the if statement where you will call your drawn functions. The first set of if statements is only to point the turtle where to go.
for report in data:  # Iterate over each report directly
        # Unpack the report month
        # Unpack the report value

You'll need to find a way to set the y coordinates for where it draws. Once you get this working, you can think of the other requirements. Go through making the program in an iterative process. Focus on getting one requirement done at a time.

Example

Here, a list named shopping_list is defined. Each item in the list is another list containing two elements:

The first element is the name of the item (e.g., "apple", "banana", "chips"). The second element is the quantity of that item.

shopping_list = [["apple", 2], ["banana", 3], ["chips", 18]]

This for loop iterates through each item in the shopping_list. Inside the loop, the current item is printed to the console.

for item in shopping_list:
    print(item) #check what is the item that I am seeing
    if(item[0] == "apple"):
        print("scanning",item[1], "apples")

The output of this loop will be:

['apple', 2]
['banana', 3]
['chips', 18]

Within the loop, there's an if statement to check if the first element of the current item is "apple". If it is, the code prints a message indicating that the apples are being scanned along with the quantity.

    if(item[0] == "apple"):
        print("scanning",item[1], "apples")

When the loop runs, it will match the "apple" item in the shopping_list and print:

scanning 2 apples

Adding from Arrays

You can use a variable to keep track of the total quantity and update it inside the loop.

total_value = 0  # Variable to store the sum of all values

Starting from here you can figure out where in your code you should put the counter.

total_quantity += item[1]

Stopping the Loop

You can try doing this by using the break function but make sure that your program doesn't end before it should. An example on how to use the break function can be found below

# A list of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Loop through the list
for num in numbers:
    print(num)

    # If the number is greater than 5, exit the loop
    if num > 5:
        print("Number is greater than 5. Breaking out of the loop.")
        break