Reputation: 3
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
Upvotes: 0
Views: 7624
Reputation: 2813
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')
Upvotes: 1