Ex 1: Cost of Cablenet

In this tutorial session, you will learn:

  • how to draw a flowchart

  • how to translate a flowchart to pseudocode using comments in Python

  • how to write basic Python code, including variable, object type, if condition, print, list, for loop, mathematical operators

Question:

Suppose you have designed a cable-net made from steel cables. You are going to fabricate them and estimate the total cost. There are 2 different prices for cables longer than 3 meters and shorter than 3 meters, 5 Fr and 3 Fr correspondingly. Now you need to count how many cables are above 3 meters and how many are below, to calculate the cost.

Here is the length of all the cables in your cable-net.

cable

length

1

1.6 m

2

3.6 m

3

2.4 m

4

3.4 m

5

2.7 m

6

2.8 m

7

3.3 m

8

3.1 m

9

3.7 m

10

1.8 m

11

1.8 m

12

2.6 m

Solution Part 1: check the length of one cable

Let's first break this problem down into small steps. Firstly, you could pick one cable and check whether its length is larger than 3 m. Secondly, you could repeat the first step to check all the cables, and then multiply the number of cables with the cost.

1. Draw Flowchart

The first step can be represented in the flowchart.

2. Write pseudocode

Now let's try to write the code to solve this problem. Firstly, we could convert our flowchart to pseudocode, a plain-English version of the flowchart.

Open your Visual Studio Code and create a New File. Now type the flowchart line by line, then save the file as .python.

pick one cable
if length larger than 3?
    long cable 
else
    short cable

We could turn our pseudocode into comments, which would help us to organize the logic when we start to write code, as well as in the future to keep track of and to understand the code. Select all the text and press ctrl + /. You will see the # showing up in front of your text.

# pick one cable
# if length larger than 3?
    # long cable 
# else
    # short cable

3. Write your code

We could turn the pseudocode line by line into code.

# pick one cable
cable_length = 1.6
# if length bigger than 3?
if cable_length > 3:
    # long cable 
    print("This is a long cable.")
# else
else:
    # short cable
    print("This is a short cable.")

When we pick one cable, we need to tell the computer the length of our cable as an input. Here, we create a variable called cable_length, and we assign it to the value of the cable length:1.6, a float number. Then we use an if statement to check the condition: whether the length is bigger or smaller than 3 m. Under different conditions, our program would print the outcome as an output.

Click the green triangle to execute the code Then you would get This is a short cable. in your terminal.

Now let's check the second cable. Change line 2:cable_length = 3.6, and run the code, you should get This is a long cable..

# pick the second cable
cable_length = 3.6 
# if length bigger than 3?
if cable_length > 3:
    # long cable 
    print("This is a long cable.")
# else
else:
    # short cable
    print("This is a short cable.")

Solution Part 2: check lengths of all the cables

Let's complete the program. You need to repeat the whole process of checking the cable length until you have classified all cables. You could use another condition in your flowchart for the repetition instructions. After you have checked all the cables in your cable-net, you could count the amount in two length types and calculate the total cost.

1. Draw Flowchart

2. Write the pseudocode

pick one cable in the cable-net
    if length larger than 3?
        long cable amount + 1
    else
        short cable amount + 1
repeat until the all the cables are checked
calculate total cost

3. Write your code

Here we need to input all the lengths of our cables. Instead of multiple length variables, we could store them in a collection - a list. List items are ordered, or in other words, indexed, the first item has index [0], the second item has index [1] etc.

cablenet = [1.6, 3.6, 2.4, 3.4, 2.7, 2.8, 3.3, 3.1, 3.7, 1.8, 1.8, 1.8, 2.6]
print(cablenet[0])  # 1.6
print(cablenet[1])  # 3.6
print(cablenet[-1])  # 2.6

To iterate over a list, we could use a for loop.

for cable in cablenet:
    print(cable)

Now we go through the list, check the cable length one by one, and count the number of the corresponding type. Thus, we need to initiate the counter of two types at the beginning of our code. Here, we create two new variables: long_cable_count, short_cable_count, and set their value to 0.

# ==============================================================================
# Input
# ==============================================================================
cablenet = [1.6, 3.6, 2.4, 3.4, 2.7, 2.8, 3.3, 3.1, 3.7, 1.8, 1.8, 1.8, 2.6]
long_cable_count = 0
short_cable_count = 0
long_cable_price = 5
short_cable_price = 3

# ==============================================================================
# Program
# ==============================================================================
# pick one cable in the cable-net
# repeat until the all the cables are checked
for cable in cablenet:
    # if length bigger than 3?
    if cable > 3:
        # long cable amount + 1
        long_cable_count += 1
    # else
    else:
        # short cable amount + 1
        short_cable_count += 1
# calculate total cost
total_cost = long_cable_count * long_cable_price + short_cable_count * short_cable_price

# ==============================================================================
# Output
# ==============================================================================
print("Total cost is", total_cost, "Fr.")

We should get Total cost is 49 Fr. when executing the code. When our code is finished, we can clean the comments to improve the readability.

Solution Part 3: modify your cable lengths

The cable length list we use as input can be modified because list items are changeable. For example, if we realize that we have omitted one cable that is 4 m, we could use append to add it at the end of our list. Then a 4-meter-cable will be added to the end of the list.

cablenet.append(4.0)

You could also modify a value in the cable-net list by referring to the item's index. For example, we find out the 3rd cable has the wrong length. Then we can find it's location: index 2 in the list, and change the value.

cablenet[2] = 2.5

Important: python lists are 0-indexed. So the first element is 0, the second is 1, so on.

If you need to delete a cable you could either delete it by its index. For example, we want to delete the 4th cable in the list, whose index is 3.

cablenet.pop(3)

The cable can also be deleted by its value. If there are duplicated values, this method only removes the first matching element.

cablenet.remove(3.3)

Exercise: cables of 3 different length type

Suppose there are three different prices for the cables: 2 Fr. for cables shorter than 2 m; 3 Fr. for cables between 2 m and 3 m; 5 Fr. for cables longer than 3 m. Could you modify your code and calculate the total cost?

Hint:

You need to classify 3 types of cables. When you run into a situation where you have several conditions, you can place as many elif conditions as necessary between the if condition and the else condition.

Answer:

# ==============================================================================
# Input
# ==============================================================================
# cable length of the cable-net
cablenet = [1.6, 3.6, 2.4, 3.4, 2.7, 2.8, 3.3, 3.1, 3.7, 1.8, 1.8, 1.8, 2.6]

# ==============================================================================
# Program
# ==============================================================================
# initiate the cable class amount
long_cable_amount = 0
mid_cable_amount = 0
short_cable_amount = 0

# price of cable
short_cable_price = 2
mid_cable_price = 3
long_cable_price = 5


# check length of the cable
for cable in cablenet:
    if cable < 2:
        short_cable_amount += 1
    elif 2 <= cable <= 3:
        mid_cable_amount += 1
    else:
        long_cable_amount += 1

# calculate total cost
total_cost = long_cable_amount * long_cable_price + mid_cable_amount * mid_cable_price + short_cable_amount * short_cable_price

# ==============================================================================
# Output
# ==============================================================================
print("Total cost is", total_cost, "Fr.")

Total cost is 42 Fr.

Last updated