Back to Freecodecamp

Step 4

curriculum/challenges/english/blocks/workshop-bill-splitter/6982684f3a25f379e195a5fc.md

latest1.8 KB
Original Source

--description--

Now that you have stored the individual costs, you can calculate the total. In Python, you use the addition operator + to sum values together.

The += operator adds a value to an existing variable and updates it at the same time. For example:

py
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.

js
({
       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.

js
({
    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--

py
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--