Python Forum

Full Version: Dictionary python program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I’m trying to write a script where I create a dictionary with some initial keys and values. I made some of those values above 10 and some less than 10.

sales = {"beef" : 4, "catfish" : 8, "alfredo" : 12, "peppers" : 18}
print(sales)

Now I want to add 2 to each value in the above sales dictionary that I created and print out the new dictionary to verify that the values changed. And then finally, print all dictionary entries with a value less than 10.
what have you tried (code)?
(Jan-05-2019, 03:06 PM)Larz60+ Wrote: [ -> ]what have you tried (code)?

sales = {"beef" : 4, "catfish" : 8, "alfredo" : 12, "peppers" : 18}
print(sales)

And I’m stuck.
There’s still nothing similar to what I need to do. I need a simple code addition to mine. Please add to the code snippet that I previously posted.
you really haven't made much of an effort.
Is this a homework assignment?
to add to an item, use:
sales['beef'] += 2
(Jan-05-2019, 05:53 PM)Larz60+ Wrote: [ -> ]you really haven't made much of an effort.
Is this a homework assignment?
to add to an item, use:
sales['beef'] += 2

I have made much of an effort. If you don’t want to help then you don’t have to and no, it’s not an assignment. I’m trying to learn. I added something but I would like it to print out the entire dictionary with the new value and not just the new values.

for key in sales.keys():
print(sales[key] + 1)
needs indentation, this code does not modify the dictionary, only the value after retreving
for key in sales.keys():
    print(sales[key] + 1)
A better way:
>>> for key, value in sales.items():
...     print('Item: {:10s} sales: {:6d}'.format(key, value))
... 
Item: beef       sales:      4
Item: catfish    sales:      8
Item: alfredo    sales:     12
Item: peppers    sales:     18
>>>