我正在尝试将字典选择传递给类函数以进一步处理。但是,当我尝试尝试时,出现错误消息,提示我缺少参数。我知道我不需要包含自我,所以我对为什么它不起作用感到困惑。它应该做:
项目用于模拟咖啡机。引用了一些我尝试过但不起作用的方法。任何帮助或建议都非常感谢
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")
发生错误:
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()
答案 0 :(得分:0)
Product
的实例具有name
,price
和recipe
属性。
在Product
中,更改
def make(self, name, price, recipe):
到
def make(self):
print(self.recipe)
在Selector
中,创建一个Product
实例,然后调用其make
方法。
更改
Product.make(*self.recipe)
到
product = Product(*self.recipe) # make an instance of Product
product.make() # call the instance method
答案 1 :(得分:0)
首先,产品类中的make方法应该是静态的,因此可以使用类名来调用它。即使您不将其设置为静态,也可以将第一个参数传递给make函数作为self,然后传递其余参数,但这没有任何意义,因为您正在使用类名调用make方法。
onPress
上面的代码产生以下输出
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)