Below is the assignment:
Create 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 go to the store.
'''Algorithm:
User enters key-value pairs that are added into a dict.
Users tells script to return total, subtotal, and key-value pairs in a nicely formatted list.'''
The use of classes and @classes should be used.
Below is the code I have written:
list = GroceryList()
TypeError: __init__() missing 2 required positional arguments: 'item' and 'price
Could someone help me figure out what I'm doing wrong? Honestly, I'm not too sure if my total and subtotal calculations are correct, and if the key and value pairs of {item:price} are being taken in and added to a dictionary properly. Also, any suggestions for better formatting of the output? Thanks!
Create 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 go to the store.
'''Algorithm:
User enters key-value pairs that are added into a dict.
Users tells script to return total, subtotal, and key-value pairs in a nicely formatted list.'''
The use of classes and @classes should be used.
Below is the code I have written:
class GroceryList: def __init__(self, item, price): self = {} self.item = item self.price = price @classmethod def addToList(self): for item, price in self(): self.append(item, price) @classmethod def subtotal(self): subtotal = 0 for item in self(): subtotal = (subtotal + self.item) return subtotal @classmethod def total(self): total = 0 for item in self(): total = (self.item + (self.item * .07)) return total @classmethod def printList(self): for item, price in self(): return(item, price) '''Test list''' list = GroceryList() list.append('milk', 2.49) list.append('eggs', 2.00) list.append('bread', 1.29) print(list.Total()) print(list.Subtotal()) print(list.returnList())I tried using the test list above to test if my code was working properly and I keep getting this error message: Traceback (most recent call last):
list = GroceryList()
TypeError: __init__() missing 2 required positional arguments: 'item' and 'price
Could someone help me figure out what I'm doing wrong? Honestly, I'm not too sure if my total and subtotal calculations are correct, and if the key and value pairs of {item:price} are being taken in and added to a dictionary properly. Also, any suggestions for better formatting of the output? Thanks!