英文:
Missing 1 required positional argument - 'Self'?
问题
我有一个特定类的以下方法:
def make_payment(self, cost):
和主文件中的以下内容:
print(money_machine.make_payment(drink.cost))
为什么会返回这个错误?(我正在参加一个代码教程,他的代码似乎没问题)
TypeError: MoneyMachine.make_payment()缺少1个必需的位置参数:'cost'
主文件:
> from menu import Menu, MenuItem
> from coffee_maker import CoffeeMaker
> from money_machine import MoneyMachine
>
> """1print report
> 2check resources sufficies
> process coins
> check transaction successful
> make coffee
> """
> coffee_maker = CoffeeMaker()
> menu = Menu()
> money_machine = MoneyMachine
>
> is_on = True
> while is_on:
>
> print(menu.get_items())
> order = input("你的订单:")
> if order == '关闭':
> is_on = False
> elif order == "报告":
> coffee_maker.report()
> else:
> drink = menu.find_drink(order)
> if coffee_maker.is_resource_sufficient(drink):
> if money_machine.make_payment(drink.cost):
> coffee_maker.make_coffee(drink)
货币机器:
class MoneyMachine:
货币单位 = "$"
硬币面值 = {
"quarters": 0.25,
"dimes": 0.10,
"nickles": 0.05,
"pennies": 0.01
}
def __init__(self):
self.profit = 0
self.money_received = 0
def 报告(self):
"""打印当前利润"""
print(f"货币: {self.货币单位}{self.profit}")
def 处理硬币(self):
"""返回插入的硬币计算得到的总额。"""
print("请插入硬币。")
for coin in self.硬币面值:
self.money_received += int(input(f"有多少{coin}?: ")) * self.硬币面值[coin]
return self.money_received
def make_payment(self, cost):
"""当付款被接受时返回True,如果不够则返回False。"""
print(self.money_received)
self.处理硬币()
if self.money_received >= cost:
找零 = round(self.money_received - cost, 2)
print(f"这是{self.货币单位}{找零}的找零。")
self.profit += cost
self.money_received = 0
return True
else:
print("抱歉,钱不够。钱已退还。")
self.money_received = 0
return False
英文:
I have the following method of a certain class:
def make_payment(self, cost):
and the following at the main file:
print(money_machine.make_payment(drink.cost))
why is it returning this? (I am following a code-along session and everything seems to be fine with his code)
TypeError: MoneyMachine.make_payment() missing 1 required positional argument: 'cost'
MAIN:
> from menu import Menu, MenuItem
> from coffee_maker import CoffeeMaker
> from money_machine import MoneyMachine
>
> """1print report
> 2check resources sufficies
> process coins
> check transaction successful
> make coffee
> """
> coffee_maker = CoffeeMaker()
> menu = Menu()
> money_machine = MoneyMachine
>
> is_on = True
> while is_on:
>
> print(menu.get_items())
> order = input("your order: ")
> if order == 'off':
> is_on = False
> elif order == "report":
> coffee_maker.report()
> else:
> drink = menu.find_drink(order)
> if coffee_maker.is_resource_sufficient(drink):
> if money_machine.make_payment(drink.cost):
> coffee_maker.make_coffee(drink)
MONEY MACHINE:
class MoneyMachine:
CURRENCY = "$"
COIN_VALUES = {
"quarters": 0.25,
"dimes": 0.10,
"nickles": 0.05,
"pennies": 0.01
}
def __init__(self):
self.profit = 0
self.money_received = 0
def report(self):
"""Prints the current profit"""
print(f"Money: {self.CURRENCY}{self.profit}")
def process_coins(self):
"""Returns the total calculated from coins inserted."""
print("Please insert coins.")
for coin in self.COIN_VALUES:
self.money_received += int(input(f"How many {coin}?: ")) * self.COIN_VALUES[coin]
return self.money_received
def make_payment(self, cost):
"""Returns True when payment is accepted, or False if insufficient."""
print(self.money_received)
self.process_coins()
if self.money_received >= cost:
change = round(self.money_received - cost, 2)
print(f"Here is {self.CURRENCY}{change} in change.")
self.profit += cost
self.money_received = 0
return True
else:
print("Sorry that's not enough money. Money refunded.")
self.money_received = 0
return False
答案1
得分: 1
根据您的评论,问题在于您没有正确实例化您的类。
总的来说,您的实例化代码应该更像这样,这是正确实例化类的方法:
money_machine = MoneyMachine()
然后调用方法会正确实例化它,并将self
传递进去,因此money_machine.make_payment(drink.cost)
会按预期工作。
实例化类实际上是调用类的__init__()
方法,这也是您在创建类的副本时需要定义任何必需参数的地方。例如:
class MoneyMachine():
def __init__(self, currency_type):
# 在这里必须用currency_type定义实例
self.currency_type = currency_type
self.profit = 0
self.money_received = 0
usa_money_machine = MoneyMachine(currency_type='USD')
英文:
Per your comments, the issue is that you are not instantiating your class correctly.
Overall your instantiation code should be more like this, which is the correct way to instantiate a class:
money_machine = MoneyMachine()
then calling methods would insatiate it correctly and you pass self
in, so money_machine.make_payment(drink.cost)
would then work as expected.
Instantiating a class is essentially calling the class's __init__()
method, this is also where you would define any required arguments if needed when creating a copy of the class. For example:
class MoneyMachine():
def __init__(self, currency_type):
# here you must instantiate with currency_type defined
self.currency_type = currency_type
self.profit = 0
self.money_received = 0
usa_money_machine = MoneyMachine(currency_type='USD')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论