python-3.x - 嗨,你能协助完成这个 python 任务吗?
问题描述
试图结账
price = {'sugar' : 45,'rice': 60,'tealeaves':450,'wheat':40,'oil':100};
ordered = {'sugar':2,'rice': 3,'tealeaves':0.5,'wheat':4,'oil':1}
total = list()
for k,v in price:
value = price[k]*kgsordered[k]
print (k,':',value)
total.append(value)
print('*'*4,'CG Grocery Store','*'*4)
print('Your final bill is ₹',total.sum())
print('Thank you for shopping with us!!')
追溯来了
回溯(最近一次通话最后):
文件“C:\Users\user\Desktop\My Python Files\curiosity gym python HW.py”,第 4 行,价格为 k,v:ValueError:解包值太多(预期为 2)
解决方案
首先,您必须使用.items()
遍历字典。
其次,您使用的是kgsordered[k]
instead of ordered[k]
,这会给您一个错误,因为kgsordered
未定义。
最后,如果你想计算一个列表中所有元素的总和,你可以这样做 sum(total)
,total
你的列表在哪里
price = {'sugar' : 45,'rice': 60,'tealeaves':450,'wheat':40,'oil':100};
ordered = {'sugar':2,'rice': 3,'tealeaves':0.5,'wheat':4,'oil':1}
total = list()
for k,v in price.items():
value = price[k]*ordered[k]
print (k,':',value)
total.append(value)
print('*'*4,'CG Grocery Store','*'*4)
print('Your final bill is ₹',sum(total))
print('Thank you for shopping with us!!')
# output
sugar : 90
rice : 180
tealeaves : 225.0
wheat : 160
oil : 100
**** CG Grocery Store ****
Your final bill is ₹ 755.0
Thank you for shopping with us!!
推荐阅读
- asp.net - 在 asp.net 中,使用 Async 技术将占用多少工作线程
- django - DRF+VueJS分页错误页数
- gensim - 如何对数百万个文档执行 doc2vec.infer_vector()?
- c++ - C++ 二进制文件 I/O 操作减慢... DB 如何处理二进制文件?
- javascript - vue + nuxt.js - 如何读取从外部请求接收到的 POST 请求参数
- cmake - 使用 EXTRA_OECMAKE 对 bitbake 配方中的 cmake 变量没有任何影响
- javascript - clearTimeout 不起作用:参数未定义(即使它是在全局范围内定义的)
- sql - Oracle 数据库与 Oracle Business Intelligence 12c 不兼容
- firebase - 无法在 Gatsby 站点的 GraphQL 中格式化日期
- rx-java2 - Rxjava Scheduler.trampoline 与 concatmap