简体   繁体   中英

increasing a key, by knowing the value - python dictionary

I need to write a code, where for every time someone buys a product, the key in the demand dictionary increases by 1. I couldn't figure out how to increase a key in a dictionary byknowing the value.

This is what I tried:

demand = {"bread":0,"butter":0,"cheese":0, "water":0,"ice cream":0}
# bread is the value and 0 is the key, which I want to increase every time

def bill(buy_lst):
    for item in buy_lst:
        demand[demand.get(item)] += 1

When I run it says:

demand[demand.get(item)] += 1
KeyError: 0

Thank you!

You seems to misunderstand key and value , in your case the string are the keys and their quantity is the value

demand = {"bread": 0, "butter": 0, "cheese": 0, "water": 0, "ice cream": 0}

So what you want is increase value given a key

buy_lst[item] += 1

To use it, you need something that ask the user one product like

def bill(buy_lst):
    item = None
    while not item:
        item = input("Please select in " + str(list(buy_lst.keys())))
        item = item if item in buy_lst else None
    buy_lst[item] += 1


if __name__ == '__main__':
    demand = {"bread": 0, "butter": 0, "cheese": 0, "water": 0, "ice cream": 0}
    bill(demand)
    print(demand)

Your issue is how you are adding to the dictionary. Notice the following:

demand = {"bread": 0, "butter": 0, "cheese": 0, "water": 0, "ice cream": 0}
buy_lst = ["bread", "butter", "bread"]

def bill(buy_lst):
    for item in buy_lst:
        print(demand.get(item))
        #demand[demand.get(item)] += 1

bill(buy_lst)

This outputs:

0
0
0

So in other words, in your:

demand[demand.get(item)] += 1

You are doing:

demand[0] += 1

Which will return the error:

Traceback (most recent call last):
  File "/Users/felipefaria/Desktop/test/main.py", line 11, in <module>
    bill(buy_lst)
  File "/Users/felipefaria/Desktop/test/main.py", line 8, in bill
    demand[demand.get(item)] += 1
KeyError: 0

Instead, you should be simply referring to the item itself within the brackets, like so:

demand = {"bread": 0, "butter": 0, "cheese": 0, "water": 0, "ice cream": 0}
buy_lst = ["bread", "butter", "bread"]

def bill(buy_lst):
    for item in buy_lst:
        demand[item] += 1

bill(buy_lst)
print(demand)

Which will output:

{'bread': 2, 'butter': 1, 'cheese': 0, 'water': 0, 'ice cream': 0}

I think you tried to mean was 'increasing value of a key that I know'. Because only values are ints so only they can be incremented. So incrementing value would be:

demand = {"bread":0,"butter":0,"cheese":0, "water":0,"ice cream":0}

def bill(buy_lst):
    for item in buy_lst:
        demand[item] += 1

For clarifying, dictionary item's first part is key and second part is value .

You might be confusing key and value. In demand , the keys are the items and the values are the counts.

#!/usr/bin/env python

demand = {"bread":0, "butter":0, "cheese":0, "water":0, "ice cream":0}
def bill(buy_list):
    for item in buy_list:
        demand[item] += 1

buy_list = ["bread", "water"]

print("Before billing:")
print(demand)

bill(buy_list)

print("After billing")
print(demand)

This prints:

Before billing:
{'bread': 0, 'butter': 0, 'cheese': 0, 'water': 0, 'ice cream': 0}
After billing
{'bread': 1, 'butter': 0, 'cheese': 0, 'water': 1, 'ice cream': 0}

Your problem is really simple. You just have to add the operator * in front of the function parameter buy_lst , so you will have *buylst . See the below code:

demand = {"bread":0,"butter":0,"cheese":0, "water":0,"ice cream":0}
# bread is the value and 0 is the key, which I want to increase every time

def bill(*buy_lst):  # see this line
    for item in buy_lst:
        demand[item] += 1  # see this line

bill("bread", "cheese") # The client buys the products `bread` and `cheese`
print(demand)
bill("bread", "cheese", "water", "butter")
print(demand)

Output

{'bread': 1, 'butter': 0, 'cheese': 1, 'water': 0, 'ice cream': 0}
{'bread': 2, 'butter': 1, 'cheese': 2, 'water': 1, 'ice cream': 0}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM