Lessons ·
Deciding and repeating
Make a program decide with if, then repeat with for and while, with Awa's shop.
Transcript
Awa now keeps her prices in variables, but she would like her program to give a discount, all by itself, to customers who buy a lot. To do that, we must teach it to decide, and that is the role of the if condition.
She starts by storing the purchase amount, here 60,000 francs.
Then she tells it: if the purchase reaches 50,000 francs? Exactly, and Python then checks this condition. Since 60,000 is indeed greater than or equal to 50,000, the answer is true.
What depends on a condition is called a block of code, and Python recognizes it by its indent of four spaces. A block can contain one or several lines. Here, the block that runs when the condition is true is a single line, line 3.
Finally, the keyword else introduces the block to run when the condition is false, and here this block is line 5. Since the condition is true, Python displays the discount and completely ignores this block.
And if she wants a small discount for medium purchases? In that case she adds an elif condition, which means otherwise if, and this time let's take a purchase of 30,000 francs.
Python tests the cases in order, and 30,000 does not reach 50,000, so it skips the first block, which is line 3.
It then moves on to the next case, and this time 30,000 is indeed above 20,000.
So it runs the block of this condition, which is line 5 and displays the small discount, and then it stops there. Just pay attention to one thing: to test equality, we write two equal signs, because a single one is used to give a value.
Now, if she has hundreds of sales, she surely won't write one addition per sale? No, because that is exactly why the for loop exists, and we are going to build it piece by piece.
Awa first prepares a variable called total, which starts at zero.
Then she writes the keyword for, followed by a variable, sale, and the list to go through. This line means: for each sale in the list.
Next comes the block of the loop, the one that is repeated on each turn. Here, this block is a single line, line 3, where the plus equals operator adds the sale to the total.
Look at what happens on the first turn: sale is 1500, and the total goes from zero to 1500.
On the second turn, sale takes the next value, 2300, and the total climbs to 3800.
On the third turn, we add 800, and the total reaches 4600.
Since there are no more sales to go through, the loop stops, and Python leaves the block to move on to line 4, which is no longer indented and displays the total.
I have also seen the range function in code, what is it for? It produces a series of numbers when we want to repeat something a certain number of times.
Be careful though, because range of 5 starts at zero and stops just before 5, which does make five numbers, but without the 5 itself.
And when we don't know in advance how many turns it will take? For example, if Awa sets aside 15,000 francs each month to reach 100,000 francs. She then prepares her savings and her month counter, both at zero.
This is the right case for the while loop, which repeats as long as a condition stays true. Before each turn, Python checks whether the savings are still under 100,000.
As long as that is true, it runs the block of the loop, which this time is two lines, lines 4 and 5. So it adds 15,000 francs and one more month, then it goes back to check.
In the seventh month, the savings reach 105,000 francs, the condition becomes false and the loop stops.
We only have to make sure that something changes on each turn, because otherwise the condition stays true and the loop never stops.
Two more instructions are useful for keeping control of a loop. With break, we leave it immediately, for example as soon as Awa finds a product that is out of stock.
The first two stocks are not zero, so the loop continues, but on the third one, the condition is true. Python then runs its block, which is lines 3 and 4: it displays the shortage, then break stops the loop, without even looking at the last product.
And if she only wants to ignore a wrong line, without stopping everything? Then she uses the continue instruction, which skips the current turn and moves on to the next one.
When the amount is negative, the block of the condition, which is line 4, skips the end of the turn. So this amount is never added, and the total stays correct.
The best thing now is to try without watching the video again, because it is by searching for yourself that all this will stay with you. In the lesson, you will write these conditions and loops, then spot the mistake in code written by an artificial intelligence.
Voices: Google Chirp 3 HD (Despina, Enceladus).
Photos : One Idea LLC, StockSnap, CC0

