Home > Back-end >  __init__ within class supposedly missing arguments?
__init__ within class supposedly missing arguments?

Time:11-27

class FoodItem:
    def __init__(self, item_name, amount_fat, amount_carbs, amount_protein, num_servings):
        self.item_name = "None"
        self.amount_fat = 0.0
        self.amount_carbs = 0.0
        self.amount_protein = 0.0
        self.num_servings = 0.0
       
    def get_calories(self, num_servings):
        # Calorie formula
        calories = ((self.fat * 9)   (self.carbs * 4)   (self.protein * 4)) * num_servings;
        return calories
       
    def print_info(self):
        print('Nutritional information per serving of {}:'.format(self.name))
        print('   Fat: {:.2f} g'.format(self.fat))
        print('   Carbohydrates: {:.2f} g'.format(self.carbs))
        print('   Protein: {:.2f} g'.format(self.protein))

if __name__ == "__main__":
    
    food_item1 = FoodItem()
   
    item_name = input()
    amount_fat = float(input())
    amount_carbs = float(input())
    amount_protein = float(input())
   
    food_item2 = FoodItem(item_name, amount_fat, amount_carbs, amount_protein)
      
    num_servings = float(input())
      
    food_item1.print_info()
    print('Number of calories for {:.2f} serving(s): {:.2f}'.format(num_servings, 
                          food_item1.get_calories(num_servings)))
                           
    print()
                           
    food_item2.print_info()
    print('Number of calories for {:.2f} serving(s): {:.2f}'.format(num_servings, 
                          food_item2.get_calories(num_servings)))

Results in the error:

Traceback (most recent call last):
  File "main.py", line 22, in <module>
    food_item1 = FoodItem()
TypeError: __init__() missing 5 required positional arguments: 'item_name', 'amount_fat', 'amount_carbs', 'amount_protein', and 'num_servings'

I'm not spotting an obvious error, but I'm new to initializing classes. The error seems to say that I'm missing arguments within the original init, but considering they were initialized to 0/'none' values, I don't understand that.

Perhaps someone can catch the error?

CodePudding user response:

You have to initialize your class with ALL the arguments: item_name, amount_fat, amount_carbs, amount_protein and num_servings.

# here, you have to provide the arguments
food_item1 = FoodItem()
...
# and here, you are missing the 'num_servings' argument
food_item2 = FoodItem(item_name, amount_fat, amount_carbs, amount_protein)

Just in case, you can provide default values to an argument like so:

# here, the argument will default to '0' if you do not provide a value.
class Example: 
    def __init__(self, argument=0): 
        self.argument = argument

example = Example()
print(example.argument)

>>> 0

CodePudding user response:

Your code is not saying what to match for example self.amount_fat with which argument you are talking about. You should write: self.amount_fat = amount_fat

And then announce was self.amount_fat is, otherwise your program will not know where to read.

def __init__(self, item_name, amount_fat, amount_carbs, amount_protein, num_servings):
    self.item_name = item_name
    self.amount_fat = amount_fat
    self.amount_carbs = amount_carbs
    self.amount_protein = amount_protein
    self.num_servings = num_servings

You can then add what you coded initially.

  • Related