Question
A Python program managing a school vending machine keeps the current inventory in a list variable called inventory with tuples (product_str, stock_int, sold_int, price_float). product_str
A Python program managing a school vending machine keeps the current inventory in a list variable called inventory with tuples (product_str, stock_int, sold_int, price_float). product_str is the name of the product; stock_int is the quantity still in stock (an int), sold_int is the quantity sold (int), and price_float is the sell price -- a float number. For example, the inventory list at some time may look like this:
[('Snickers Bar', 20, 30, 1.5), ('Tic Tac', 18, 10, 1.0), ('Cheetos', 43, 15, 2.0)]
a) Write ONE expression with a list comprehension that returns a list with the total sales $$ amount for each product. Name the returned list sales_lst. The total sale for a product tuple is sold_int*price_float. For the example inventory list above, the desired list with product sales is:
[('Snickers Bar', 45.0), ('Tic Tac', 10.0), ('Cheetos', 30.0)]
b) Write ONE expression that uses sales_lst from part a) with a list comprehension that returns the total sales across all products. For the products in the above example, the total sales figure is 85.0.
Hint: use the builtin sum() function.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started