Practice: Order Summary

Practice: Order Summary

This exercise combines the section: variables, arithmetic, conversion, and f-strings.

A customer orders several copies of one item. Your code computes the cost and builds a one-line summary.

Exercise

The starter code creates item, price, and quantity_text. Note that quantity_text is a string.

  1. Create quantityquantity_text converted to an int.
  2. Create subtotalprice times quantity.
  3. Create tax — 10% of subtotal (multiply by 0.1).
  4. Create totalsubtotal plus tax.
  5. Create summary using an f-string, so that it holds exactly:

5 x notebook: 22.0 dollars

Use quantity, item, and total inside the braces. 6. Print summary.

Run your code to see the output, then press Submit.

Hints

  • Convert quantity_text before doing any arithmetic with it.
  • Build total from subtotal and tax, not from typed-in numbers.
  • Compare your printed line character by character with the required one.

Tests

import unittest


class TestOrderSummary(unittest.TestCase):
    def test_quantity_is_the_int_5(self):
        self.assertEqual(quantity, 5)
        self.assertIsInstance(quantity, int)

    def test_subtotal_is_20(self):
        self.assertAlmostEqual(subtotal, 20.0)

    def test_tax_is_2(self):
        self.assertAlmostEqual(tax, 2.0)

    def test_total_is_22(self):
        self.assertAlmostEqual(total, 22.0)

    def test_summary_has_the_exact_text(self):
        self.assertEqual(summary, "5 x notebook: 22.0 dollars")
item = "notebook"
price = 4.0
quantity_text = "5"

quantity = int(quantity_text)
subtotal = price * quantity
tax = subtotal * 0.1
total = subtotal + tax
summary = f"{quantity} x {item}: {total} dollars"

print(summary)
Solution hidden. Give it a real try first.

Sign in to join the discussion.

No comments yet. Be the first to ask a question.

main.py
Console
Press Run to execute your code, or Submit to test and complete this problem.