Resolved: Question about output isolation in exercise 8.6 iteration of dictionaries?
How to make the code 1: Print only the final result, not every step of the process? 2: If no value is cheaper/more expensive than 5, to print "No value was cheaper/higher than 5"? Here for example it gives me 3 sentences:
No products were priced at 5 dollars or above The products of 5 dollars or above costed: 50 No products were priced at 5 dollars or above
But i don't want that. I want it to print directly the result, or if there is no such product in the dictionary, to print "None of the values was within the parameters". Some suggested modules i found on the internet were things like all and larger but i don't know how to implement them. Thanks for your help in advance.
Professor, if you dont help with our questions, how will we learn?
Hi Panagiotis!
Thanks for reaching out.
You can use something like this:
prices = {
"box_of_spaghetti" : 4,
"lasagna" : 5,
"hamburger" : 2
}
quantity = {
"box_of_spaghetti" : 6,
"lasagna" : 10,
"hamburger" : 0
}
money_spent=0
for i in prices:
if prices[i] >= 5:
money_spent += prices[i] * quantity[i]
print("The products of 5 dollars or above costed", money_spent)
If you add the second condition in this loop, every time it has a price less than 5, it will print you the message.
Hope this helps.
Best,
Tsvetelin
Thank you professor i found the solution. I nest the next if conditional, out of the for loop. like that:
for i in prices:
if prices[i] >= 5:
mon_spent_up5 += prices[i] * quantity[i]
print("The products of 5 dollars or above costed:", mon_spent_up5)
if mon_spent_up5 == 0:
print("No product was in that price range.")
Thus if after all iterations it is still 0, it gives back "No product was in that price range". Cause when i was nesting it in the initial loop, it always gave at least the first iteration as a "No product in range", because the first in the dictionary, it was a sub 5 product. Thanks for your input.
Would be glad to hear your helping feedback in the rectangle exercise as well:
https://learn.365datascience.com/question/sir-can-you-please-help-me-with-the-rectangle-exercise-of-7-4/
Hi Panagiotis!
Thanks for reaching out.
Yes, please refer to the link provided. Thank you.
Kind regards,
Martin