Chapter 0 · Section 7 practice
Check and Explain Your Work
Work from question 1 to 10. Write your code, click Run Code, review the result, then click Submit attempt. An attempt may be wrong: reference answers become available after you submit all 10. Each Python box is independent. Submission progress is saved in this browser.
1. Fix a name
Warm-up. Python names are case-sensitive. Repair the misspelled name so this prints 20.
Write your answer, then run it.
2. Fix mixed types
Warm-up. The intended numeric answer is 350. Convert the text input before adding.
Write your answer, then run it.
3. Find the missing colon
Warm-up. Repair the syntax so a score of 65 prints Pass.
Write your answer, then run it.
4. Repair indentation
Build your skills. Put the first print inside the if block and keep Done outside. The result at False should be only Done.
Write your answer, then run it.
5. Compare rather than assign
Build your skills. The condition should test whether the label equals YES. Repair the operator, then print Approved at the given input.
Write your answer, then run it.
6. A calculation can run and be wrong
Build your skills. The desired profit is revenue minus cost, not their sum. Fix the code, print the result, and add a comment explaining the mistake.
Write your answer, then run it.
7. An excluded endpoint
Build your skills. The program must visit years 1, 2, 3, and 4. Fix the range and print all four years.
Write your answer, then run it.
8. A total that keeps resetting
Challenge. Move the initialization so all three prices contribute to the final total. Print the correct total.
Write your answer, then run it.
9. Choose the right conversion
Challenge. A suggested fix uses int for a decimal price and still fails. Repair the conversion, print the bill with two decimals, then test price_text=“8.75” yourself.
Write your answer, then run it.
10. Find a units mistake
Challenge. The input price is 250 HK cents per item, and 3 items are bought. Repair the conversion and variable name so the printed bill is in HK dollars. Add a comment with a hand check.
Write your answer, then run it.
Reference answers are locked until all 10 attempts are submitted.
1. Fix a name
price = 10
print(price * 2)Expected output
20Price has not been defined; the existing name is price.
2. Fix mixed types
cash = 100
online_text = "250"
print(cash + int(online_text))Expected output
350A numeric calculation requires numeric values. Here the whole-number text can use int.
3. Find the missing colon
score = 65
if score >= 60:
print("Pass")Expected output
PassA block header ends with a colon.
4. Repair indentation
raining = False
if raining:
print("Take umbrella")
print("Done")Expected output
DoneIndentation changes which instructions belong to the condition.
5. Compare rather than assign
label = "YES"
if label == "YES":
print("Approved")Expected output
ApprovedUse == in the equality test, not assignment =.
6. A calculation can run and be wrong
revenue = 500
cost = 300
profit = revenue - cost
print(profit)
# The original code added spending instead of subtracting it.Expected output
200No exception is needed for a logic error. Check the meaning of the formula.
7. An excluded endpoint
years = 4
for year in range(1, years + 1):
print(year)Expected output
1
2
3
4The stop value is excluded, so it must be one greater than the last desired year.
8. A total that keeps resetting
prices = [10, 20, 30]
total = 0
for price in prices:
total = total + price
print(total)Expected output
60Initialization belongs before the loop; otherwise only the final item survives.
9. Choose the right conversion
price_text = "12.50"
quantity_text = "2"
price = float(price_text)
quantity = int(quantity_text)
print(f"HK${price * quantity:.2f}")Expected output
HK$25.00Decimal price text needs float. Changing the price to 8.75 should produce HK$17.50.
10. Find a units mistake
price_cents = 250
quantity = 3
price_hkd = price_cents / 100
bill_hkd = price_hkd * quantity
print(f"HK${bill_hkd:.2f}")
# 250 cents = HK$2.50; three items cost HK$7.50.Expected output
HK$7.50A correct multiplication can still use the wrong units. Convert cents to dollars before interpreting the bill.