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.
The starter code creates item, price, and quantity_text. Note that quantity_text is a string.
quantity — quantity_text converted to an int.subtotal — price times quantity.tax — 10% of subtotal (multiply by 0.1).total — subtotal plus tax.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.
quantity_text before doing any arithmetic with it.total from subtotal and tax, not from typed-in numbers.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)
Sign in to join the discussion.
No comments yet. Be the first to ask a question.
Press Run to execute your code, or Submit to test and complete this problem.