Last answered:

28 Jul 2020

Posted on:

27 Jul 2020

0

Functions, dictionaries & while

In the last lecture from "Introduction to Python" exercise which involves the dictionaries, I tried to use the following:
I want the following code to give the total price when use call the function with the price being equal or less that the parameter. However the output should be 74 and it is give me 50, being the exact same as the lasagna. How can I change the code to give me a output in this case of 74 being all prices below or equal to the parameter 5?
prices = {
"box_of_spaghetti" : 4,
"lasagna" : 5,
"hamburger" : 2
}
quantity = {
"box_of_spaghetti" : 6,
"lasagna" : 10,
"hamburger" : 0
}
money_spent = 0

def Price_while(Price):
money_spent = 0

for item in prices:
while Price <= prices[item]:
money_spent += (prices[item] * quantity[item])
return money_spent

Price_while(5)

Output: 50

1 answers ( 0 marked as helpful)
Instructor
Posted on:

28 Jul 2020

0

Hi Andre!
Thanks for reaching out.
This Python function returns a single value. So, if you execute Price_while(5), you will be referring to the "lasagna" as you say. It is the only meal whose price is greater than or equal to 5, therefore we have 5*10=50 currency units spent.
Then, if you execute Price_while(4), you will be referring to the price spent for a box of spaghetti, which will be 4*6=24.
To obtain the result from the lecture, please use the code provided there. Thank you.
Hope this helps but please feel free to get back to us should you need further assistance. Thank you.
Best,
Martin

Submit an answer