微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我不能减去字典值?

如何解决我不能减去字典值?

我如何减去字典的值,, 这是代码

from collections import Counter 


no_of_shoes = int(input('input number of shoes'))

d=[]
for i in range(no_of_shoes):
    stock = list(map(int,input("enter offerd size").split(" ")))
    d+=stock
s=Counter(d)
customer= int(input("enter the number of customers"))

total_money = 0
for i in range (0,customer):
    size,money = map(int,input("enter the size with price").split(' '))
    if size in s.keys() :
                total_money+=money
                s[size]-=1

    else:
       print(" this size not exist")
    


print("the total mony we'v got is:",total_money)

在这里我想要鞋子法令 -1,这样如果顾客再次想要同样的鞋子,按摩就会出现(这个 sizw 不存在)

   total_money = 0
    for i in range (0,customer):
        size,input("enter the size with price").split(' '))
        if size in s.keys() :
                    total_money+=money
                    s[size]-=1

解决方法

你不需要这个: s=Counter(d)

只需将您的列表 d 与鞋码一起使用。您可以使用 d.remove(size)

从列表中轻松删除元素

编辑:如果您需要计数器解决方案,则需要修复 IF 条件:


from collections import Counter 


no_of_shoes = int(input('input number of shoes'))

d=[]
for i in range(no_of_shoes):
    stock = list(map(int,input("enter offerd size").split(" ")))
    d+=stock
s=Counter(d)
customer= int(input("enter the number of customers"))

total_money = 0
for i in range (0,customer):
    size,money = map(int,input("enter the size with price").split(' '))
    if s[size] > 0:
        total_money+=money
        s[size]-=1

    else:
       print(" this size not exist")
    


print("the total mony we'v got is:",total_money)```

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。