Testing decimal results with round()

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

Testing decimal results with round()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. In Python, what does 0.1 + 0.2 == 0.3 give?
A True, because 0.1 + 0.2 is exactly 0.3
B False, because the float result is slightly inexact
2. Which test is the better way to check a decimal result against an expected value?
A round(actual, 2) == expected
B actual == expected
3. A test expects 0.3 but the code returns 0.30000000000000004. The test uses round(actual, 2) == 0.3. What is the verdict?
A PASS, because round gives 0.3
B FAIL, because the tiny extra digits are still compared
4. Which line correctly tests a price total of 19.99 + 5.01 that should be 25.0?
A round(total) == 25.0 and ignore the cents
B round(total, 2) == 25.0
5. Why do we choose the number of decimal places in round(actual, 2)?
A To match the precision the expected value needs, such as cents for money
B Because round() only works with exactly 2 decimal places

Match the Pairs

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

Left columnRight column
1round(actual, 2) == expected Around(0.1 + 0.2, 1) == 0.3
20.1 + 0.2 == 0.3 BComparing a float result directly with == gives False because 0.1 + 0.2 is 0.30000000000000004. Fix: print(round(0.1 + 0.2, 2) == 0.3)
3A test for a price calculation rounds the actual result to 2 decimal places (cents) before comparing it with the expected value CEvaluates to False in Python because the float sum is really 0.30000000000000004
4Choose the number of decimal places to match the precision of the expected value, such as 1 decimal place for an expected 0.3 DThe expected value has more decimal places than the rounded result, so they never match and the test fails. Fix: round(actual, 2) == 0.33
5print(0.1 + 0.2 == 0.3) ERound the actual result to 2 decimal places, then compare it with the expected value to test a decimal result
6round(actual, 2) == 0.333333 Fprint(round(price * 3, 2) == 7.5)

Definitions

round(actual, 2) == expected

Fill in the Blanks

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

1.
print(_____(0.1 + 0.2, 2) == 0.3)
2.
actual = 0.1 + 0.2 print(round(actual, _) == 0.3)
3.
actual = 0.1 + 0.2 print(round(actual, 2) __ 0.3)
4.
expected = 0.3 if round(0.1 + 0.2, 2) == ________: print("PASS")
5.
actual = 0.1 * 3 expected = 0.3 print(round(______, 2) == expected)

Spot the Error

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

Question 1
A exp = 0.3
B act = 0.1 * 0.2
C print(round(act, 2) == exp)
Question 2
A total = 19.99 + 5.01
B ok = round(total, 2) = 25.0
C print(ok)
Question 3
A p = 0.1 / 3
B exp = 0.3
C print(round(p, 2) == exp)
Question 4
A actual = 1.1 + 2.2
B if actual == 3.3:
C print('PASS')
D else:
E print('FAIL')
Question 5
A expected = 0.3
B actual = 0.1 + 0.2
C r = round(actual, 2)
D if r != expected:
E print('PASS')
F else: print('FAIL')

Answer Key

Testing decimal results with round()
Section A: Quick Questions
Q1
B — False, because the float result is slightly inexact
Q2
A — round(actual, 2) == expected
Q3
A — PASS, because round gives 0.3
Q4
B — round(total, 2) == 25.0
Q5
A — To match the precision the expected value needs, such as cents for money
Section B: Match the Pairs
Q1
round(actual, 2) == expected → Round the actual result to 2 decimal places, then compare it with the expected value to test a decimal result
Q2
0.1 + 0.2 == 0.3 → Evaluates to False in Python because the float sum is really 0.30000000000000004
Q3
A test for a price calculation rounds the actual result to 2 decimal places (cents) before comparing it with the expected value → print(round(price * 3, 2) == 7.5)
Q4
Choose the number of decimal places to match the precision of the expected value, such as 1 decimal place for an expected 0.3 → round(0.1 + 0.2, 1) == 0.3
Q5
print(0.1 + 0.2 == 0.3) → Comparing a float result directly with == gives False because 0.1 + 0.2 is 0.30000000000000004. Fix: print(round(0.1 + 0.2, 2) == 0.3)
Q6
round(actual, 2) == 0.333333 → The expected value has more decimal places than the rounded result, so they never match and the test fails. Fix: round(actual, 2) == 0.33
Section C: Definitions
Q1
round(actual, 2) == expected — Floats can be slightly inexact (0.1 + 0.2 is not 0.3), so round the actual result to a set number of decimal places before comparing it with the expected value.
Section D: Fill in the Blanks
Q1
print(round(0.1 + 0.2, 2) == 0.3)
Q2
actual = 0.1 + 0.2 print(round(actual, 2) == 0.3)
Q3
actual = 0.1 + 0.2 print(round(actual, 2) == 0.3)
Q4
expected = 0.3 if round(0.1 + 0.2, 2) == expected: print("PASS")
Q5
actual = 0.1 * 3 expected = 0.3 print(round(actual, 2) == expected)
Section E: Spot the Error
Q1
B — The test is meant to check 0.1 + 0.2 against 0.3. Multiplying gives 0.02, so the check would fail for the wrong reason. It should be act = 0.1 + 0.2.
Q2
B — A single = is assignment, not comparison, so this is a syntax error. Use == to compare: ok = round(total, 2) == 25.0.
Q3
A — The expected value 0.3 comes from 0.1 * 3. Dividing gives about 0.033, so the test would fail. It should be p = 0.1 * 3.
Q4
B — 1.1 + 2.2 is stored as 3.3000000000000003, so comparing directly with == gives False. Round first: if round(actual, 2) == 3.3:.
Q5
D — != means 'not equal', so PASS would print when the values differ, which reverses the test. It should be if r == expected:.