Python Forum
Help with homework assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with homework assignment
#1
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:

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!
Reply


Messages In This Thread
Help with homework assignment - by grayarea3 - Feb-20-2019, 09:40 PM
RE: Help with homework assignment - by ichabod801 - Feb-20-2019, 10:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I am completely lost on this homework assignment a36 1 1,754 Feb-21-2022, 06:01 AM
Last Post: buran
  saving issue on homework assignment russoj5 2 1,988 Oct-26-2020, 01:53 PM
Last Post: russoj5
  Homework Assignment Help pinku018 3 3,094 Jun-08-2018, 01:09 PM
Last Post: j.crater
  Homework Assignment Help sphedicl 3 3,307 Jun-08-2018, 12:26 PM
Last Post: pinku018

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020