Loading…
Loading…
In this lesson, students are introduced to the if…else and if…elif…else statements in Python for multi-way decision making. Both statements can execute the code inside the body of the if/elif when certain conditions are met. Code inside the body of else will be executed if neither of the condition is met. To explore conditional constructs, students will develop a computational model to simulate the commodity trading and practice the ‘buy low, sell high’ strategy.
In this lesson, students are introduced to the if…else and if…elif…else statements in Python for multi-way decision making. Both statements can execute the code inside the body of the if/elif when certain conditions are met. Code inside the body of else will be executed if neither of the condition is met. To explore conditional constructs, students will develop a computational model to simulate the commodity trading and practice the ‘buy low, sell high’ strategy.
Further exploration of Python conditional statements (elif and else)
How to import modules (random)
Using the Random() function to get a random number from a range
This lesson expands on the previous lesson by introducing the if…else and if…elif…else statements in Python for multi-way decision making
Both statements can execute the code inside the body of the if/elif when certain conditions are met.
Code inside the body of else will be executed if neither of the condition is met
To explore conditional constructs, students will develop a computational model to simulate the commodity trading and practice the ‘buy low, sell high’ strategy
Welcome to this Python Coding lesson! Before you begin, take a few moments to let us know your thoughts or feedback on how we can improve this lesson in order to better enhance the learning experience that is delivered in your classroom!
Click here or follow the form below to complete the feedback survey once you've completed this lesson. Thank you!
Students should have a laptop or device with mBlock 5 installed (desktop version or cloud-based version) and mBlock Python editor (mLink) installed. Click here to download the Python editor (mLink).
Worksheet: Lesson 5 Triangle Area Calculator
Example Program: Lesson 5 Triangle Area Calculator
1. Let's begin by familiarizing ourselves with the key points of this lesson:
Import the random module in Python by using ‘import random’
Use the random() function to simulate the price change in the commodity market
The if…elif…else statement and indentation
The difference between the ‘if…if…if’ form and the ‘if…elif…else’ form
2. We will explore these concepts using a financial example of ‘buy low, sell high’
One of the methods to make a profit is to ‘buy low, sell high’ in the market. You buy something when its price is low and sell it when the price is high.
The profits come from price inflation. Commodity prices fluctuate from time to time due to the change in demand in the market.
However, the ‘buy low, sell high’ method is only a simplified model. In the real-world scenarios, it is hard to do so because most of the people cannot predict the change in prices precisely. The ‘buy low, sell high’ model introduced in this lesson is merely used to teach the conditional construct. This simplified model cannot be assumed as a scientific model in finance and economics.
3. Have students fill out the ‘What I Know’ column of the K-W-L chart before the class.

1. When you open the editor for the first time you will see these explanations mapping the interface. Encourage students to thoroughly read through it. Below are the screen captures if you ever need to reference them again.
Figure 1: To connect your CyperPi

Figure 2: Creating Projects and Project files (see How to Create, Open, Import, or Export Project Files)

Figure 3: Editing Area

Figure 4: Console Commands to run code and more

Figure 5: Terminal (Console) to display output from Editing Area

Figure 6: Libraries containing example program. Feel free to explore these at your leisure

Figure 7: Click "Tutorials" to view these helpful instructions anytime

1. Introduce a simplified model of ‘buy low, sell high’.
Say: Today, we are all traders. We will explore a simple but powerful rule of commodity trading – ‘buy low, sell high’ – to see how it works in the commodity market. We will also create a simple commodity market simulator in Python to ‘buy low, sell high’.
2. Briefly explain the ‘buy low, sell high’ strategy.
Say: Market prices of commodities fluctuate over time due to the exchange. Some buyers want to purchase commodities from others, while some sellers want to sell commodities at acceptable profits
Explain: The simplest way is to ‘buy low’ and ‘sell high’. Buyers decide a price at which they would like to buy a commodity, while sellers propose a price at which they would like to sell a commodity. These decisions affect the current market.
Give an example: Suppose that you purchase a commodity at the price of $100. The price of this commodity rises 20% two weeks later, and you sell it at this price (i.e. $120) in the commodity market. You then earn $20 in this transaction. The price of this commodity drops 20% two weeks after you sell it. You buy it at the price of $96 ($120-$120×20%).
1. Have students read "Activity 1" in the example program

2. Instruct students to write down their predictions and annotate the code on the worksheet
3. Ask students to think about the questions below:
What is the control flow structure? Find out the syntax.
What does ‘import random’ mean?
What are the values of the variable ‘price_a’ and ‘price_b’?
4. Ask students to run the example program and check against their predictions
5. Explain the use of if…elif…else statements.
6. Review the if statements learnt in the last lesson first.
Say: Like the BMI Calculator application we developed last time, the Commodity Simulator application also uses a selection construct. However, the syntax is different.
Explain the if…elif…else statement: The if…elif…else statement is used for multi-way decision making. The ‘elif’ is short for else if. If the condition for the ‘if’ is ‘False’, the program then checks the condition for the ‘elif’. If both conditions are ‘False’, the program executes the indented body of the ‘else’.
7. Ask students to explain the selection operation of the Commodity Simulator program and draw the flowchart. The same as last lesson, students should use the proper shapes when constructing their flowcharts.

8. Show an example of the if…elif…else control flow for reference:

9. After students have finished drawing their flowcharts, explain why and how to generate random numbers.
Task 1: Comment the code on the worksheet. Write notes after the hash mark of the line and briefly explain the function.
Task 2: Investigate the conditions of the statements. How does the program compare the posted price with the traded price?
Task 3: What is the condition of the ‘else’. Write down its condition.
1. Have students work in pairs to modify the example program by completing the task below:
2. Use only if statements instead to rewrite the example program. Compare the two programs. Is there anything different?

3. Instruct students to investigate the difference between using the if statement syntax and using the if…elif…else statement
Note: Pay attention to the logical procedures when running the scripts. Consider how the computer may evaluate the conditions
Have students fill out the K-W-L chart
Remind students to always comment their code, regardless of the simplicity

Make a simple calculator that takes in two numbers and asks the user what mathematical operation they would like to perform
Utilize the if statements taught in this lesson to perform the decision making
Algebra
C2.1 use coding to demonstrate an understanding of algebraic concepts including variables, parameters, equations, and inequalities
C2.2 create code by decomposing situations into computational steps in order to represent mathematical concepts and relationships, and to solve problems
C2.3 read code to predict its outcome, and alter code to adjust constraints, parameters, and outcomes to represent a similar or new mathematical situation
B1: ICS20 Arithmetic Operations
B1.1 use correct terminology to describe programming concepts;
B1.2 describe the types of data that computers can process and store
B1.4 determine the expressions and instructions to use in a programming statement, taking into account the order of operations
B1.5 identify situations in which decision and looping structures are required;
B1.6 describe the function of Boolean operators
B2: Writing Programs
B2.1 use a visual problem-solving model to plan the content of a program;
B2.2 use variables, expressions, and assignment statements to store and manipulate numbers and text in a program
B2.3 write keyboard input and screen output statements that conform to program specifications;
B2.4 write a program that includes a decision structure for two or more choices
B3: Code Maintenance
B3.1 write clear and maintainable code using proper programming standards
B3.3 use a tracing technique to understand program flow and to identify and correct logic and run-time errors in a computer program;
B3.4 demonstrate the ability to validate a computer program using test cases