ይህ ትምህርት ገና ወደ አማርኛ አልተተረጎመም፤ ስለዚህ በእንግሊዝኛ ቀርቧል። የእንግሊዝኛውን ገጽ ክፈቱ
if, elif, else
In the last lesson you asked Python yes-or-no questions. Now you'll act on the answers: run some lines only when a condition is true, choose between two paths, or pick one of many. By the end you'll know exactly which path Python takes, including the ordering bug that breaks most grade calculators.
Running lines only sometimes
What does this print? The fare is 180.
ውጤቱን ገምቱ
Decide before you look. Guessing wrong is how this sticks.
fare = 180
if fare > 200:
print("Too expensive")
print("Done")Python prints
Done
fare > 200 is False, so Python skips the indented line under the if. print("Done") isn't indented, so it isn't part of the if and runs either way.
An if asks a question. When the answer is True, Python runs the indented lines
under it; when it's False, Python skips them. The line with if ends with a colon,
and the lines that belong to it are indented by four spaces. Those lines are a
.
The block ends where the indentation ends.
Indentation isn't decoration in Python; it's how Python knows what belongs to the if.
Two paths, then many
else gives the other path: its block runs when the if condition is False. For
more than two paths, add elif ("else if") branches between them. Python checks the
conditions from the top and runs only the first block whose condition is true.
Step through it and watch which lines Python visits.
One path through the chain
Press Next to run one line at a time and watch each name change.Each row is one step: the line that just ran, and every name's value after it.
| Name | Value after line 1 |
|---|---|
| score | 72new |
Output so far
Step 1 of 4
Step 1 of 4. Line 1: score = 72. score is now 72.
| Step | Line | score | Printed |
|---|---|---|---|
| 1 | 1 | 72 | |
| 2 | 2 | 72 | |
| 3 | 4 | 72 | |
| 4 | 5 | 72 | B |
72 is also at least 50, but line 6 is never checked. Once a branch runs, Python jumps past the whole chain. That makes the order of the conditions matter. This grade scale is an example (universities differ); predict what a score of 90 gets.
ውጤቱን ገምቱ
Decide before you look. Guessing wrong is how this sticks.
score = 90
if score >= 50:
print("C")
elif score >= 70:
print("B")
elif score >= 85:
print("A")Python prints
C
90 >= 50 is True, and it's checked first, so Python prints C and skips the rest of the chain. The A branch is never reached.
Every score of 50 or more stops at the first branch. The fix is to ask the hardest question first: order the checks from the highest grade down.
score = 90
if score >= 85:
print("A")
elif score >= 70:
print("B")
elif score >= 50:
print("C")
else:
print("F")In the editor, press Escape then Tab to move on.
Try 85, 84, 50, and 49. A boundary is where bugs hide.
ጥያቄ
temp is 30. The code is: if temp > 20: print Warm, elif temp > 25: print Hot, else: print Cold. What prints?
An if is checked once
An if isn't a rule that keeps watching a value. It's checked once, when Python
reaches it. Step through this:
Step through it
Press Next to run one line at a time and watch each name change.Each row is one step: the line that just ran, and every name's value after it.
| Name | Value after line 1 |
|---|---|
| balance | 100new |
Output so far
Step 1 of 4
Step 1 of 4. Line 1: balance = 100. balance is now 100.
| Step | Line | balance | Printed |
|---|---|---|---|
| 1 | 1 | 100 | |
| 2 | 2 | 100 | |
| 3 | 4 | 20 | |
| 4 | 5 | 20 | Balance: 20 |
When line 2 ran, balance was 100, so the warning was skipped. Line 4 drops it to 20,
but Python never goes back up. To check again, you'd need another if after line 4.
An if inside an if
The Ethiopian calendar has 12 months of 30 days, then Pagume, the 13th month, with
5 days, or 6 in a leap year. An Ethiopian year is a leap year when dividing it by 4
leaves 3, so year % 4 == 3 (lesson 1.2's %). 2019 is one. A block can hold
another if:
month = 13
year = 2019
if month <= 12:
days = 30
else:
if year % 4 == 3:
days = 6
else:
days = 5
print(days)In the editor, press Escape then Tab to move on.
It works, but each level pushes the code further right. When an else holds nothing
but another if, join them into an elif:
month = 13
year = 2019
if month <= 12:
days = 30
elif year % 4 == 3:
days = 6
else:
days = 5
print(days)In the editor, press Escape then Tab to move on.
Same answers, one level flatter. Change the year to 2020, then the month to 5.
መልመጃ
Choose a data bundle
Ask how many MB someone needs and print the smallest bundle that covers it. The bundles are examples: Small is 100 MB for 10 birr, Medium is 500 MB for 35 birr, Large is 2,000 MB for 100 birr. Above 2,000 MB, print that no bundle is big enough. Test 100, 101, 2000, and 2500 too.
How many MB do you need? 700
Large: 2,000 MB for 100 birrYour code
mb = int(input("How many MB do you need? "))
# Check from the smallest bundle upIn the editor, press Escape then Tab to move on.
One answer per line, given to input() in order.
መፍትሄውን አሳይHide the solution
This is one way to solve it, not the only one. If yours prints the same thing, it works.
mb = int(input("How many MB do you need? "))
if mb <= 100:
print("Small: 100 MB for 10 birr")
elif mb <= 500:
print("Medium: 500 MB for 35 birr")
elif mb <= 2000:
print("Large: 2,000 MB for 100 birr")
else:
print("No bundle is big enough")ዋና ዋና ነጥቦች
ifruns its indented block only when its condition isTrue; the colon and the indentation are required.- In an
if/elif/elsechain, only the first true branch runs, so order matters. - Order grade checks from the highest down, and test every boundary.
- An
ifis checked once, when Python reaches it; changing a value later doesn't rerun it. - When an
elseholds only anotherif, flatten it intoelif.