Ladders and mixed-up lines
Answers are for parents and teachers.
1
Write a Python program: ask for the temperature of an oven. Below 100 print Cool, below 180 print Warm, and otherwise print Hot.
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): temp = int(input("Oven temperature? ")) / if temp < 100: / [indent] print("Cool") / elif temp < 180: / [indent] print("Warm") / else: / [indent] print("Hot")
2
Say what is printed for 9, 10 and 11.
Python
1stars = int(input("Stars? ")) 2if stars > 10: 3 print("Top") 4elif stars > 5: 5 print("Middle") 6else: 7 print("Low")
9: Middle. 10: Middle. 11: Top.
3
Explain the difference between a syntax error and a logic error. Give an example of each from this week.
A syntax error breaks the rules of Python, so the program will not start, for example a missing colon or else if. A logic error lets the program run but gives a wrong answer, for example conditions in the wrong order.
4
Draw the flowchart for a program that prints Cheap for a price below 5, Fair for a price below 20 and Dear otherwise.
Start, INPUT price, a diamond price < 5? with yes to OUTPUT Cheap. On the no arrow a second diamond price < 20? with yes to OUTPUT Fair and no to OUTPUT Dear. Then Stop.
5
A friend writes if, then elif, but forgets the colon after elif. What message will Python show, and does the program start?
Python shows SyntaxError: expected ':'. The program does not start, because it is a syntax error found before anything runs.