1 Star 0 Fork 0

fanyangchu/PythonCode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Grocery calculator 1.46 KB
一键复制 编辑 原始数据 按行查看 历史
FavianaNoronha 提交于 2020-10-05 20:51 . Create Grocery calculator
'''This will be a Python script that functions as a grocery calculator. It will take in key-value pairs for items
and their prices, and return the subtotal and total, and can print out the list for you for when you're ready to
take it to the store!'''
'''Algorithm:
1. User enters key-value pairs that are added into a dict.
2. Users tells script to return total, subtotal, and key-value pairs in a nicely formatted list.'''
#Object = GroceryList
#Methods = addToList, Total, Subtotal, returnList
class GroceryList(dict):
def __init__(self):
self = {}
def addToList(self, item, price):
self.update({item:price})
def Total(self):
total = 0
for items in self:
total += (self[items])*.07 + (self[items])
return total
def Subtotal(self):
subtotal = 0
for items in self:
subtotal += self[items]
return subtotal
def returnList(self):
return self
'''Test list should return:
Total = 10.70
Subtotal = 10
returnList = {"milk":4, "eggs":3, "kombucha":3}
'''
List1 = GroceryList()
List1.addToList("milk",4)
List1.addToList("eggs", 3)
List1.addToList("kombucha", 3)
print List1.Total()
print List1.Subtotal()
print List1.returnList()
#*****************************************************
print
#*****************************************************
List2 = GroceryList()
List2.addToList('cheese', 7.49)
List2.addToList('wine', 25.36)
List2.addToList('steak', 17.64)
print List2.Total()
print List2.Subtotal()
print List2.returnList()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/fanych/pythoncode.git
git@gitee.com:fanych/pythoncode.git
fanych
pythoncode
PythonCode
master

搜索帮助