CIS 2020 · Section 3.5 · Worked examples

More flowchart 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

Should you water the plant?

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.

Flowchart: should you water the plant Start, input whether the soil is dry and whether it is raining. Decide whether the soil is dry and it is not raining. Yes leads to watering the plant. No leads to not watering it. Both lead to output and end. Start Input: soilIsDry, isRaining soilIsDry and not isRaining ? Yes Water the plant No Do not water Output: decision
One diamond, one compound condition. If either half of the 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

Coffee loyalty punch card

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.

Flowchart: coffee loyalty punch card discount Start, input number of punches. First decision checks fewer than five punches, resulting in full price. Otherwise a second decision checks fewer than nine punches, resulting in ten percent off. Otherwise the coffee is free. All three outcomes lead to output and end. Start Input: punches punches < 5? Yes Full price No punches < 9? Yes 10% off No Free coffee Output: price
Same two-diamond shape as the parking garage flowchart in the Module 6 review, proof that the shape belongs to the pattern, not the problem.
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

Movie theater popcorn combo pricing

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.

Flowchart: popcorn combo pricing, a decision nested inside a decision Start, input student status and combo size. First decision checks whether the customer is a student. Each branch, yes and no, leads to its own second decision checking whether the combo is large, producing one of four prices. All four prices lead to output and end. Start Input: isStudent, comboSize isStudent? Yes No large? Y $6 N $4 large? Y $9 N $6 Output: price
Two more diamonds, one tucked entirely inside the Yes branch and one entirely inside the No branch of the first. Nesting, unlike chaining, puts a whole second decision inside a single branch instead of after it.
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

Rock, paper, scissors: who wins?

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.

Flowchart: rock paper scissors winner Start, input both players' choices. First decision checks whether the choices are equal, resulting in a tie. Otherwise a second decision checks whether player one's choice beats player two's choice using a combined or of three and conditions, resulting in either player one or player two winning. All three outcomes lead to output and end. Start Input: p1choice, p2choice p1choice == p2choice ? Yes It's a tie No (p1='rock' and p2='scissors') or (p1='paper' and p2='rock') or (p1='scissors' and p2='paper')? Yes Player 1 wins No Player 2 wins
The second diamond holds a single condition built from three 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)