我正在尝试将字典选择传递到类函数中,以便进一步处理。但是当我尝试时,我得到一个错误,说我缺少一个参数。我知道我不需要包含self,所以我不明白为什么它不起作用。它应该做到:
该项目是模拟咖啡机。引用了一些我尝试过但不起作用的东西。任何帮助或建议都将不胜感激
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()
首先,product类中的make方法应该是静态的,这样就可以用类名调用它。即使不使其成为静态的,也可以将第一个参数传递给make function作为self,然后传递其余的参数,但这没有任何意义,因为您使用类名调用make方法。你知道吗
上面的代码生成以下输出
Product
的实例具有name
、price
和recipe
属性。你知道吗在
Product
中,更改至
在
Selector
中,生成一个Product
实例,然后调用其make
方法。你知道吗改变
至
相关问题 更多 >
编程相关推荐