Last answered:

04 Feb 2023

Posted on:

28 Dec 2022

0

issue with code

if i run this code, it's showing different answer like
24
74
74

so please sugest the correct way to execute the code

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

06 Jan 2023

0

Hi Sathish!
Thanks for reaching out.

Could you please share your code as a post? This can help us assist you better. Thank you.

Looking forward to your answer.
Best,
Tsvetelin

Posted on:

23 Jan 2023

0

prices={"cabage":12,"carrot":10,"spinach":5}
quantity={"cabage":10,"carrot":12,"spinach":18}

money_spent=0
for i in prices:
    money_spent=money_spent+(prices[i]*quantity[i])
    print(money_spent)

Why i am getting this like this pls suggest to correct the code.

120
240
330

Instructor
Posted on:

04 Feb 2023

0

Hi Sathish!

Thanks for your reply.

Indeed, 12 cabbages that cost 10 currency units each make up to 12 * 10 = 120. Analogously for the carots, you have 120. So, 120+120 equals 240. When you add the amount spent on (stems of) spinach, 240 + 90 = 330.

Therefore, for the quantities and prices provided, your output seems correct. 

If you wouldn't like the money spent to accumulate and obtain the money spent on each vegetable separately, you can make the following modification:

money_spent=0
for i in prices:
    money_spent=(prices[i]*quantity[i])
    print(money_spent)

Hope this helps.
Best,
Martin 

Submit an answer