问题描述
我如何减去字典的值,, 这是代码
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)```