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
#2
It's all a big mess. The error you are getting is because GroceryList() calls GroceryList.__init__, which specifies three parameters: self, item, and price. The self parameter is provided automatically, but you don't provide an item and price parameter. There are a number of ways you could fix that, but you still have other problems.

But self = {} is just invalid, even if it doesn't cause an error. self refers to the current instance the method is working on. You have just obliterated that, and replaced it with an empty dictionary. That dictionary will just disappear at the end of the method call.

You need to create an attribute that holds the dictionary, probably called prices. I wouldn't start with an item and a price, that doesn't make sense. Then none of the other methods should be class methods. They should all operate on the attribute with the dictionary (self.prices if you called it prices), not on self directly.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I am completely lost on this homework assignment a36 1 1,720 Feb-21-2022, 06:01 AM
Last Post: buran
  saving issue on homework assignment russoj5 2 1,943 Oct-26-2020, 01:53 PM
Last Post: russoj5
  Homework Assignment Help pinku018 3 3,050 Jun-08-2018, 01:09 PM
Last Post: j.crater
  Homework Assignment Help sphedicl 3 3,250 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