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 Pythonhow 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
.
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.
3. Write your code
We could turn the pseudocode line by line into code.
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.
.
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
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.
To iterate over a list, we could use a for
loop.
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.
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.
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.
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.
The cable can also be deleted by its value. If there are duplicated values, this method only removes the first matching element.
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:
Total cost is 42 Fr.
Last updated