Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary python program
#1
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.
Reply
#2
what have you tried (code)?
Reply
#3
(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.
Reply
#4
see: https://python-forum.io/Thread-Create-Dy...ctionaries
Reply
#5
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.
Reply
#6
you really haven't made much of an effort.
Is this a homework assignment?
to add to an item, use:
sales['beef'] += 2
Reply
#7
(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)
Reply
#8
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
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Running the Regressive Imagery Dictionary program... treegreen 3 2,741 May-02-2019, 10:45 PM
Last Post: treegreen
  Convert List of Dictionary to dictionary of dictionary list in python kk230689 2 53,981 Apr-27-2019, 03:13 AM
Last Post: perfringo
  creating a username and pword program using a #def statement and #dictionary zcode12 3 3,177 Oct-14-2018, 04:41 AM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020