Last answered:

01 Aug 2024

Posted on:

20 Jul 2024

0

Need more understanding

I tried to solve it without using variable money_spent. Here is the solution::


sum([prices[item]*quantity[item] for item in quantity if item in prices.keys()])


I just wonder is there alternative or better or shortest way of solving it without using money_spent and still apply all taught in this course?


Thanks in advance

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

01 Aug 2024

0

Hi Albert!

Thanks for reaching out.

Please accept my apologies for the delayed response. 

Of course, there is often more than a single solution to a given Python task. However, in the course, we want to ensure that the students have the skills to understand and work with with a large variety of skills, including all fundamentals. 

For this particular exercise, we would like to ensure that the student can work with conditionals and loops well. The list comprehensions you are referring to are part of our Intermediate Python Programming course: 

https://learn.365datascience.com/courses/intermediate-python-programming/using-list-comprehensions/

They are preferable to use but a Python user should also know how to read and write iterations. 

Hope this helps.
Kind regards,
Martin


Submit an answer