0

I'm trying to pass a dictionary selection into a class function to do further things with. When I try though, I get an error saying I'm missing a parameter. I know I don't need to include self, so I'm confused as to why it isn't working. It should do:

  1. Receives choiceindex from other part of code taking user input
  2. Uses the choiceIndex to select from the dictionary
  3. Pass in the 3 pieces of the dictionary to the Product class

Project is to simulate a Coffee Machine. Quoted out are a few of the things I've tried but don't work. Any help or advice is much appreciated

class Product(object):

    def __init__(self,name,price,recipe):
        self.name = name
        self.price = price
        self.recipe = recipe

    def getPrice(self):
        return self.price

    def make(self, name, price, recipe):
        print(self.recipe)
        #for item in recipe:
        #    print("dispensing", item)

class Selector(object):

    def __init__(self):
        #self.Product = Product()
        self.cashBox = CashBox
        self.product = []
        #self.products.append(Product.

    def select(self, choiceIndex):
        recipes = {
            1 : ["black","35","cup coffee water"],
            #1 : ["black",35,"cup coffee water"],
            #1 : self.Product.make("black",35,"cup coffee water"),
            2 : ["white",35,["cup", "coffee", "creamer", "water"]],
            3 : ["sweet",35,["cup", "coffee", "sugar", "water"]],
            4 : ["white&sweet",35,["cup", "coffee", "sugar", "creamer", "water"]],
            5 : ["bouillon",35,["cup bouillonPowder", "water"]]
        }
        if choiceIndex in range(1,len(recipes)+1):
            self.choiceIndex = choiceIndex
            self.recipe = recipes.get(choiceIndex)
            print(self.recipe,"Great selection")
            Product.make(*self.recipe)
        else:
            print("That selection does not exist")

Error occurred:

Exception has occurred: TypeError
make() missing 1 required positional argument: 'recipe'
  File "C:\Users\Tanner Harmer\Desktop\Coffee2\CashBox.py", line 101, in select
    Product.make(*self.recipe)
  File "C:\Users\Tanner Harmer\Desktop\Coffee2\CashBox.py", line 41, in oneAction
    Selector.select(self,int(words[1]))
  File "C:\Users\Tanner Harmer\Desktop\Coffee2\CashBox.py", line 107, in main
    while m.oneAction():
  File "C:\Users\Tanner Harmer\Desktop\Coffee2\CashBox.py", line 113, in <module>
    main()
T_lastname
  • 122
  • 9
  • `Product.make(*self.recipe)` can't be called directly like that. You would need to consturct the `Product(name, price, recipe)` and then call `.make()` on it. – Rithin Chalumuri Nov 03 '19 at 02:39
  • Please format the code - select it and type `ctrl-k`. [Formatting posts](https://stackoverflow.com/help/formatting) ... [Formatting help](https://stackoverflow.com/editing-help) ... [Formatting Sandbox](https://meta.stackexchange.com/questions/3122/formatting-sandbox) – wwii Nov 03 '19 at 02:40
  • `Product().make(*self.recipe)` – Matt Eding Nov 03 '19 at 02:40
  • For calling like that `Product.make` you need a static or class method – geckos Nov 03 '19 at 02:42
  • [Unpacking Argument Lists](https://docs.python.org/3/tutorial/controlflow.html?highlight=argument%20unpacking#unpacking-argument-lists) – wwii Nov 03 '19 at 02:44
  • @T_lastname Could you please post your error message? Because I don't get any such parameter error with unpacking lists. – Venkata Rathnam Nov 03 '19 at 02:46
  • 1
    Definitely will use formatting in the future, sorry still pretty new thanks for the tips, I'll try and figure out how to work that in. A lot of what I know is just stuff I've been shown, I don't know a ton of the terms. Unpacking Argument Lists looks super helpful, thank you and yes I'll get that error up right now – T_lastname Nov 03 '19 at 02:49

2 Answers2

0

Instances of Product have name, price, and recipe attributes.

In Product, change

def make(self, name, price, recipe):

to

def make(self):
    print(self.recipe)

In Selector, make a Product instance then call its make method.

Change

Product.make(*self.recipe)

to

product = Product(*self.recipe)    # make an instance of Product
product.make()    # call the instance method
wwii
  • 23,232
  • 7
  • 37
  • 77
0

First of all the make method in the product class should be static so you can call it with class name. Even if you don't make it static you can pass the first argument to make function as self and then the rest of the arguments but that doesn't make any sense because you are calling make method with class name.

class Product(object):

    def __init__(self,name,price,recipe):
        self.name = name
        self.price = price
        self.recipe = recipe

    def getPrice(self):
        return self.price

    @staticmethod
    def make(name, price, recipe):
        print(recipe)
        for item in recipe:
            print("dispensing", item)

class Selector(object):

    def __init__(self):
        #self.Product = Product()
        self.cashBox = 0
        self.product = []
        #self.products.append(Product.

    def select(self, choiceIndex):
        recipes = {
            1 : ["black","35","cup coffee water"],
            2 : ["white",35,["cup", "coffee", "creamer", "water"]],
            3 : ["sweet",35,["cup", "coffee", "sugar", "water"]],
            4 : ["white&sweet",35,["cup", "coffee", "sugar", "creamer", "water"]],
            5 : ["bouillon",35,["cup bouillonPowder", "water"]]
        }
        if choiceIndex in range(1,len(recipes)+1):
            self.choiceIndex = choiceIndex
            self.recipe = recipes.get(choiceIndex)
            print(self.recipe,"Great selection")
            Product.make(self.recipe[0], self.recipe[1], self.recipe[2])
        else:
            print("That selection does not exist")

selectedProduct = Selector()
selectedProduct.select(1)

The above code produces following output

['black', '35', 'cup coffee water'] Great selection cup coffee water dispensing c dispensing u dispensing p dispensing dispensing c dispensing o dispensing f dispensing f dispensing e dispensing e dispensing dispensing w dispensing a dispensing t dispensing e dispensing r

Mateen Kiani
  • 2,869
  • 2
  • 19
  • 29