Skip to content

Latest commit

 

History

History
54 lines (34 loc) · 1.46 KB

exercise_1.md

File metadata and controls

54 lines (34 loc) · 1.46 KB

Unit 3 Practice

Exercise 1

Create a dictionary of fruits. The name of each fruit will be the key and the value will be its price.

1.1

Using the fruits dictionary:

  • Display the price of a fruit
  • Add a fruit to the dictionary, print the dictionary to verify its creation
  • Remove a fruit from the dictionary, print the dictionary to verify its deletion

Output:

Apples cost $0.65 each
Added 'grapes': {'apple': 0.65, 'banana': 0.5, 'guava': 0.33, 'grapes': 0.99}
Removed 'banana': {'apple': 0.65, 'guava': 0.33, 'grapes': 0.99}

1.2

Loop through the dictionary and display each item and its price.

Output:

Fruit Prices
------------
Apple: 0.65
Banana: 0.5
Guava: 0.33

1.3

Create another dictionary to represent a shopping_basket.

The keys of this dictionary will be fruit names and the values will be the quantity of each item in the shopping_basket.

Loop through the shopping_basket and add up the grand_total. With each iteration of the loop, also print out how many of each fruit are in the shopping_basket and the sub_total for that fruit.

Display all the sub_totals and the grand_total to look like a receipt in the terminal.

Output:

4 Apples: $2.6
6 Bananas: $3.0
8 Guavas: $2.64
-------------
Grand Total: $8.24

Exercise 1 solution