Readability improvements in code

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Readability improvements in code

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which variable name makes code more readable?
A x1
B studentAge
2. Which choice shows good indentation inside an if statement?
A The lines inside the if are indented the same amount
B The lines inside the if are lined up randomly
3. The same 5 lines of code are copied in four places in a program. How can you make it more readable?
A Leave the copies so each place is self-contained
B Move them into one function and call it four times
4. True or false: Readable code uses clear names, consistent layout, short functions and helpful comments.
A True
B False
5. Which function name is the most readable?
A calculateTotalPrice()
B doStuff()

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1total_price = price * quantity AUnclear single-letter names do not tell the reader what the values mean. Use names like subtotal and discount.
2def show_total(price, qty): return price * qty Bdef greet(name): print("Hello " + name) greet("Ava") greet("Liam")
3Removing repeated code by putting it in a function that you call each time CThe same line is repeated many times. Use a function or a loop so the code is written once.
4Consistent indentation and spacing show which lines belong together Dif score >= 50: print("Pass") else: print("Try again")
5x = a * b z = x - 5 EA meaningful variable name that tells the reader what the value stores
6print("Hi Ava") print("Hi Liam") print("Hi Mia") print("Hi Noah") FA short function with consistent indentation that does one clear job

Definitions

readable code

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
total = price * ________ print("Total:", total)
2.
___ border(): print("-" * 20)
3.
_ Add 10 percent GST to the price gst = price * 0.1
4.
___ mark in marks: print(mark)
5.
def addgst(price): ______ price * 1.1 print(addgst(50))

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A def total_price(price, qty):
B total = price * qty
C return total
D print(totalprice(5, 3))
Question 2
A def area(width height):
B return width * height
C room = area(4, 5)
D print(room)
Question 3
A names = ["Ava", "Liam", "Mia"]
B for name in names
C print("Hi", name)
Question 4
A def greet(name):
B return "Hi " + name
C for student in ["Ava", "Zoe"]:
D print(greet(studnet))
Question 5
A score = 85
B if score => 50:
C print("Pass")
D else:
E print("Fail")

Answer Key

Readability improvements in code
Section A: Quick Questions
Q1
B — studentAge
Q2
A — The lines inside the if are indented the same amount
Q3
B — Move them into one function and call it four times
Q4
A — True
Q5
A — calculateTotalPrice()
Section B: Match the Pairs
Q1
total_price = price * quantity → A meaningful variable name that tells the reader what the value stores
Q2
def show_total(price, qty): return price * qty → A short function with consistent indentation that does one clear job
Q3
Removing repeated code by putting it in a function that you call each time → def greet(name): print("Hello " + name) greet("Ava") greet("Liam")
Q4
Consistent indentation and spacing show which lines belong together → if score >= 50: print("Pass") else: print("Try again")
Q5
x = a * b z = x - 5 → Unclear single-letter names do not tell the reader what the values mean. Use names like subtotal and discount.
Q6
print("Hi Ava") print("Hi Liam") print("Hi Mia") print("Hi Noah") → The same line is repeated many times. Use a function or a loop so the code is written once.
Section C: Definitions
Q1
readable code — Code that people can quickly understand, using clear names, consistent indentation and spacing, short functions with no repeated code, and helpful comments.
Section D: Fill in the Blanks
Q1
total = price * quantity print("Total:", total)
Q2
def border(): print("-" * 20)
Q3
# Add 10 percent GST to the price gst = price * 0.1
Q4
for mark in marks: print(mark)
Q5
def addgst(price): return price * 1.1 print(addgst(50))
Section E: Spot the Error
Q1
D — The function is called total_price, but the call uses totalprice. Names must be used consistently, otherwise Python gives a NameError. It should be print(total_price(5, 3)).
Q2
A — The parameters need a comma between them. It should be def area(width, height):.
Q3
B — A for loop line must end with a colon. The loop replaces three repeated print lines, but it needs for name in names: to work.
Q4
D — The loop variable is student, but this line spells it studnet. A clear name only helps if it is used consistently, otherwise Python gives a NameError. It should be print(greet(student)).
Q5
B — The operator for greater than or equal to is >=, not =>. It should be if score >= 50:.