1.8 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6982684f3a25f379e195a5fc | Step 4 | 20 | step-4 |
--description--
Now that you have stored the individual costs, you can calculate the total.
Recall that the += operator adds a value to an existing variable and updates it at the same time. For example:
total = 10
total += 2 + 2 + 1
print(total) # total is now 15
Use the += operator once to add appetizers, main_courses, desserts, and drinks to running_total.
Finally, use print() to display the string Total bill so far: followed by a space and the value of running_total.
Note: You might notice that the output has more decimal digits than expected. As you learned in a previous lesson, this happens because numbers are stored in binary, and many decimal values cannot be represented exactly in this format, which leads to rounding errors.
--hints--
You should update running_total using the += operator once to add all four course costs.
({
test: () => runPython(`
import itertools
perms = itertools.permutations(['+ appetizers', '+ main_courses', '+ desserts', '+ drinks'])
values = (' '.join(perm).lstrip('+') for perm in perms)
solutions = (f'running_total += {v}' for v in values)
assert any(_Node(_code).has_stmt(s) for s in solutions)`)
})
You should print the string Total bill so far: followed by a space and the value of running_total.
({
test: () => runPython(`
sol1 = "print('Total bill so far:', running_total)"
sol2 = "print(f'Total bill so far: {running_total}')"
assert _Node(_code).has_call(sol1) or _Node(_code).has_call(sol2)`)
})
--seed--
--seed-contents--
running_total = 0
num_of_friends = 4
appetizers = 37.89
main_courses = 57.34
desserts = 39.39
drinks = 64.21
--fcc-editable-region--
--fcc-editable-region--