Dictionary python program - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Dictionary python program (/thread-15135.html) |
Dictionary python program - NLittle17 - Jan-05-2019 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. RE: Dictionary python program - Larz60+ - Jan-05-2019 what have you tried (code)? RE: Dictionary python program - NLittle17 - Jan-05-2019 (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. RE: Dictionary python program - Larz60+ - Jan-05-2019 see: https://python-forum.io/Thread-Create-Dynamic-nested-Dictionaries RE: Dictionary python program - NLittle17 - Jan-05-2019 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. RE: Dictionary python program - Larz60+ - Jan-05-2019 you really haven't made much of an effort. Is this a homework assignment? to add to an item, use: sales['beef'] += 2 RE: Dictionary python program - NLittle17 - Jan-05-2019 (Jan-05-2019, 05:53 PM)Larz60+ Wrote: you really haven't made much of an effort. 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) RE: Dictionary python program - Larz60+ - Jan-06-2019 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 >>> |