Python 我不能减去字典的值?
如何减去字典的值,, 这是密码Python 我不能减去字典的值?,python,dictionary,subtraction,Python,Dictionary,Subtraction,如何减去字典的值,, 这是密码 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) cus
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,这样,如果客户再次想要同样的鞋子,按摩就会出现(这个尺码不存在)
你不需要这个:
s=计数器(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)```
那么您就不需要
d
。使用计数器对象s
进行操作。不要使用d+=stock
对库存中的物品使用:s[item]+=1
,这样您就可以在计数器对象中保存大小和给定大小的数量。一旦你做对了,这部分代码应该会起作用:s[size]-=1
计数器用于按鞋创建字典,我们有多少像{1:2,2:3}鞋号1,我们有2个,所以当我输入1并定价时,其中许多将从2变为1,然后计数器将变为{1:1,2:3}实际上我写了s[size]-=它仍然不起作用是的,如果s[size]>0:在您的if条件下,您需要使用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)```