Python 在嵌套字典中添加项
我在自学Python,并试图用Python创建一个排序应用程序。该程序将用户给定的产品编号及其价格添加到新词典中 但是我想不出一个方法来把这些物品的价格加在一起Python 在嵌套字典中添加项,python,nested,Python,Nested,我在自学Python,并试图用Python创建一个排序应用程序。该程序将用户给定的产品编号及其价格添加到新词典中 但是我想不出一个方法来把这些物品的价格加在一起 alijst = {} kera = {1001 : {"naam" : '60 X 60 Lokeren', 'prijs' : 31.95},#the third item (31.95) is the prize and needs to be used in a calculation later
alijst = {}
kera = {1001 : {"naam" : '60 X 60 Lokeren', 'prijs' : 31.95},#the third item (31.95) is the prize and needs to be used in a calculation later
1002 : {"naam" : '40 X 80 Houtlook' , 'prijs' : 32.5},
1003 : {"naam" : '60 X 60 Beïge', 'prijs' : 29.95}}
# The below is for finding the code linked to a product
def keramisch():
global kera
global alijst
klaar = False
while klaar != True:
product = int(input("type a product code or press 0 to close: "))
if product in kera:
alijst[product] = kera[product]
else:
if product == 0:
klaar = True
# The below is what I tried for calculation (it sucks)
def berekenprijs():
global alijst
global prijslijst
for i, prijs in alijst:
print(i)
aantal = int(input("give an amount"))
totaalprijs = aantal * prijs
prijslijst[totaalprijs]
watbestellen()
berekenprijs()
如何将价格计入最后一个def?我认为您的错误在于:
for i, prijs in alijst:
这将为您提供订单代码(i
)和产品,而不是价格。然后,产品有一个属性列表,其中一个是“naam”,另一个是“prijs”
还要注意,您需要.items()
来迭代键和值
for i, prijs in alijst.items():
因此,要访问产品名称,您需要编写
print(prijs["naam"])
要获得价格,您需要
print(prijs["prijs"])
后者显然说明这里的命名有误
因此,我建议将这些变量重命名为
for productcode, product in alijst.items():
然后使用访问产品的属性
print(product["naam"])
print(product["prijs"])
还有一些问题,我将留给你练习
指未定义的变量global prijslijst
- 未定义watbestellen()。很可能您的意思是
keramisch()
什么也不做,但是由于缺少了prijslijst[totaalprijs]
,我很难理解您想要做什么prijslijst
def keramisch():
" Gets product code and verifies that it exists in kera "
while True:
product = input('Product code or blank line to close: ')
if not product:
return None # blank line entered
elif product.isnumeric():
product = int(product) # covert string to int
if product in kera:
return product
else:
print('Product code does not exist')
else:
print('Product code should be numeric, or blank line to close.')
def berekenprijs():
" Gets list of products and caculates total "
alijst = {}
while True:
# Get product
product = keramisch()
if product is None:
break
while True:
# Get Amount
aantal = input('Amount of product: ')
if not aantal.isnumeric():
print('Amount should be numeric')
else:
alijst[product] = int(aantal)
break # done with entering amount
# Calculate total
total = 0
for product in alijst:
price = kera[product]['prijs']
quantity = alijst[product]
total += quantity*price # quantity x price
print(f'Total Price is: {total}')
kera = {1001 : {"naam" : '60 X 60 Lokeren', 'prijs' : 31.95},#the third item (31.95) is the prize and needs to be used in a calculation later
1002 : {"naam" : '40 X 80 Houtlook' , 'prijs' : 32.5},
1003 : {"naam" : '60 X 60 Beïge', 'prijs' : 29.95}}
# Usage
berekenprijs()
我相信@Thomas Weller已经给出了下面的正确答案。我只想添加一条评论,指出像这样使用
global-alijst
被认为是不好的做法。您的函数可以接受参数,这(除其他原因外)比使用global
s更好,因为它允许重复使用您的函数。请注意,如果您对变量使用英文名称,您的问题将对堆栈溢出的国际受众中的大部分更有用。