Question: Write a function shop(item_dict): Itll take a dictionary of items and positive prices as input, like: { 'cookie': 5.12, 'milk': 3.17, 'apple': 1.89 } First,
Write a function shop(item_dict): Itll take a dictionary of items and positive prices as input, like: { 'cookie': 5.12, 'milk': 3.17, 'apple': 1.89 } First, use iterate over every price using item_dict.values() and assert (using assert) that all prices are positive. (Youduseassert price > 0, "Price should be positive",wherepriceisavariablecontain- ing the thing you want to check) Then, iterate over every item using item_dict.items() and ask the user how many of each they want, as an int. If the user enters a negative number, consider it to be 0. After going through every item, tell the user how many items they purchased, as well as how much their shopping cart costs, in total. Forexample,callingshop({ 'cookie': 5.12, 'milk': 3.17, 'apple': 1.89 }),theprogram will ask: cookie? And the user can type in a number, like 2: cookie? 2 The program will then ask for milk: milk? And the user will answer: milk? 0 And the program will ask for apple, as well. Then the program will output the total price. Overall, the program will behave like this (if called as shop({ 'cookie': 5.12, 'milk': 3.17, 'apple': 1.89 })),iftheuserenters2,00 def shop(item_dict): # ... cookie? 2 milk? 0 apple? 10 Total: $29.14 for 12 items Thank you for shopping! <- user enters '2' <- user enters '0' <- user enters '10' Remember to use %.2f to print out the price to only two decimal places. Assert that the total price and number of items are both non-negative, using assert. Hint: One thing you can do is follow a similar pattern that weve been doing with choice.py and get_list_of() before you use the for loop, initialize a variable and then update it (by, maybe, adding a number to it?) at every step of the loop.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
