CIS 2020 · Section 3.5 · Worked examples
The Module 6 review covers a single decision and a chain of decisions. These four go further: a decision built from an and, a decision nested inside another decision, and a chain compared against a genuinely different domain each time, so the shape of the flowchart stops being tied to any one example.
Example 1
A single diamond, but the condition inside it is a combination of two boolean values, the flowchart version of Section 3.7's and.
The rule: water the plant only if the soil is dry and it is not raining today. Both have to be true at once.
and is false, the whole diamond answers No.soilIsDry = input("Is the soil dry? (yes/no): ") == "yes"
isRaining = input("Is it raining? (yes/no): ") == "yes"
if soilIsDry and not isRaining :
decision = "Water the plant"
else :
decision = "Do not water"
print(decision)
Example 2
Three outcomes from two stacked diamonds, the same elif-chain shape as the parking garage example, applied to a completely different problem.
The rule: fewer than 5 punches pays full price. 5 to 8 punches gets 10 percent off. 9 or more punches means the coffee is free.
punches = int(input("Enter number of punches: "))
if punches < 5 :
price = "Full price"
elif punches < 9 :
price = "10% off"
else :
price = "Free coffee"
print(price)
Example 3
A decision drawn entirely inside one branch of another decision, the flowchart shape of Section 3.4's nested branches.
The rule: students get a discount, but the combo size still matters for everyone. Students pay $4 for a small combo or $6 for a large; everyone else pays $6 for a small or $9 for a large.
isStudent = input("Are you a student? (yes/no): ") == "yes"
comboSize = input("Combo size (small/large): ")
if isStudent :
if comboSize == "large" :
price = 6
else :
price = 4
else :
if comboSize == "large" :
price = 9
else :
price = 6
print("Price: $" + str(price))
Nested, not chained. Notice the inner "large?" diamond appears twice in the picture, once for students and once for everyone else, because it is a completely separate decision made inside each outer branch. An elif chain would draw as one long vertical line of diamonds instead, not two short side branches.
Example 4
Two sequential decisions, where the second condition is itself a boolean expression built from Section 3.7's or and and.
The rule: if both players choose the same thing, it's a tie. Otherwise, player 1 wins with rock against scissors, paper against rock, or scissors against paper; any other combination means player 2 wins.
and pairs joined by or, exactly the kind of expression Section 3.7 says needs parentheses to stay readable.p1 = input("Player 1, choose rock, paper, or scissors: ")
p2 = input("Player 2, choose rock, paper, or scissors: ")
if p1 == p2 :
result = "It's a tie"
elif ((p1 == "rock" and p2 == "scissors") or
(p1 == "paper" and p2 == "rock") or
(p1 == "scissors" and p2 == "paper")) :
result = "Player 1 wins"
else :
result = "Player 2 wins"
print(result)