Input N number of items from the user Add a dictionary into another dictionary (nested) Loop over items in a dictionary If statements for items in a dictionary Print statements for total cost

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Python!

Learning Objectives

  1. Input N number of items from the user
  2. Add a dictionary into another dictionary (nested)
  3. Loop over items in a dictionary
  4. If statements for items in a dictionary
  5. Print statements for total cost

Hint:: prices are in float and numberinstock is integers.

I am giving the following code:

def get_total_cost(all_products):
    total = 0.0
    ''' Write your code here'''
    return total

 

if __name__ == "__main__":
    n = int(input())
    all_products = {}
    ''' Write your code here'''

Hint: prices are in float and numberinstock is integers.
Example:
Input
3.
banana 2 10
strawberry 3 30
apple 1.5 20
Output
{ 'banana': {'price': 2.0,
'number_in_stock': 10}, 'strawberry': {'price': 3.0, 'number_in_stock':
30}, 'apple': {'price': 1.5, 'number_in_stock': 20}}
The total cost of all the products is $140.0
354684.2266598.qx3zqy7
Transcribed Image Text:Hint: prices are in float and numberinstock is integers. Example: Input 3. banana 2 10 strawberry 3 30 apple 1.5 20 Output { 'banana': {'price': 2.0, 'number_in_stock': 10}, 'strawberry': {'price': 3.0, 'number_in_stock': 30}, 'apple': {'price': 1.5, 'number_in_stock': 20}} The total cost of all the products is $140.0 354684.2266598.qx3zqy7
Instructions
You own a shop and you need to calculate the total cost if you sold all your products. In order to do that, you asked a programmer to write
you a program that takes all the products you have with their prices and number in stock. The result of the program should be the total
price of all the products in your stock.
1. Get a number N from the user for the number of products the user will enter.
2. Loop N times to get the product, price, numberinstock for each product (all in the same line) and save this information in a dictionary
with this structure:
{ 'banana'
{
price: 10,
number_in_stock: 5
}
}
Add this dictionary in a all_products dictionary.
3. After saving all the user's information, call function get_total_cost to get the total cost of all the products
4. Function get_total_cost should be implemented as follows:
4.1 Loop over the dictionary in the function parameters all_products
4.2 Check if numberinstock is greater than 0, then calculate the price of this product by numberinstock * price for this product
4.3 Add the result of step 4.2 to the total cost
4.4 Return the total cost
5. Print (outside the function) the all_products dictionary
6. Print (outside the function): "The total cost of all the products is $<total_cost>"
Hint: prices are in float and numberinstock is integers.
Transcribed Image Text:Instructions You own a shop and you need to calculate the total cost if you sold all your products. In order to do that, you asked a programmer to write you a program that takes all the products you have with their prices and number in stock. The result of the program should be the total price of all the products in your stock. 1. Get a number N from the user for the number of products the user will enter. 2. Loop N times to get the product, price, numberinstock for each product (all in the same line) and save this information in a dictionary with this structure: { 'banana' { price: 10, number_in_stock: 5 } } Add this dictionary in a all_products dictionary. 3. After saving all the user's information, call function get_total_cost to get the total cost of all the products 4. Function get_total_cost should be implemented as follows: 4.1 Loop over the dictionary in the function parameters all_products 4.2 Check if numberinstock is greater than 0, then calculate the price of this product by numberinstock * price for this product 4.3 Add the result of step 4.2 to the total cost 4.4 Return the total cost 5. Print (outside the function) the all_products dictionary 6. Print (outside the function): "The total cost of all the products is $<total_cost>" Hint: prices are in float and numberinstock is integers.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Dictionary
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education