M5) Printing cost
At a university library, print jobs of up to 10 pages are free for everyone. Longer jobs cost €0.05 per page for students and €0.10 per page for staff. A programmer wrote the rule as one conditional expression, shown in Listing 1.
Python groups nested conditional expressions from right to left: A if c1 else B if c2 else C means A if c1 else (B if c2 else C). It evaluates c1 first.
graph TD
accTitle: Printing cost, flowchart A
accDescr: Jobs of up to 10 pages cost 0. Longer jobs cost 0.05 per page for students and 0.10 per page for staff.
A((Start)) --> B[/"<i>pages</i>, <i>is_student</i>"/]
B --> C{"Is <i>pages</i><br><= 10?"}
C -- Yes --> D[/"0"/]
C -- No --> E{"Is <i>is_student</i><br>True?"}
E -- Yes --> F[/"0.05 × <i>pages</i>"/]
E -- No --> G[/"0.10 × <i>pages</i>"/]
D --> Z((End))
F --> Z
G --> Z
graph TD
accTitle: Printing cost, flowchart B
accDescr: Students pay 0 for up to 10 pages and 0.05 per page otherwise. Staff always pay 0.10 per page.
A((Start)) --> B[/"<i>pages</i>, <i>is_student</i>"/]
B --> C{"Is <i>is_student</i><br>True?"}
C -- Yes --> E{"Is <i>pages</i><br><= 10?"}
C -- No --> G[/"0.10 × <i>pages</i>"/]
E -- Yes --> D[/"0"/]
E -- No --> F[/"0.05 × <i>pages</i>"/]
D --> Z((End))
F --> Z
G --> Z
graph TD
accTitle: Printing cost, flowchart C
accDescr: Jobs of up to 10 pages cost 0.05 per page for students and 0.10 per page for staff. Longer jobs cost 0.
A((Start)) --> B[/"<i>pages</i>, <i>is_student</i>"/]
B --> C{"Is <i>pages</i><br><= 10?"}
C -- Yes --> E{"Is <i>is_student</i><br>True?"}
C -- No --> D[/"0"/]
E -- Yes --> F[/"0.05 × <i>pages</i>"/]
E -- No --> G[/"0.10 × <i>pages</i>"/]
D --> Z((End))
F --> Z
G --> Z
graph TD
accTitle: Printing cost, flowchart D
accDescr: The flowchart first asks whether the user is a student. Both branches then give 0 for up to 10 pages, and 0.05 or 0.10 per page otherwise.
A((Start)) --> B[/"<i>pages</i>, <i>is_student</i>"/]
B --> C{"Is <i>is_student</i><br>True?"}
C -- Yes --> E{"Is <i>pages</i><br><= 10?"}
C -- No --> H{"Is <i>pages</i><br><= 10?"}
E -- Yes --> D[/"0"/]
E -- No --> F[/"0.05 × <i>pages</i>"/]
H -- Yes --> D
H -- No --> G[/"0.10 × <i>pages</i>"/]
D --> Z((End))
F --> Z
G --> Z