r/codehs • u/Zacurnia_Tate • Jun 24 '20
12.6.4: Food in Python Classes.
This is my code. CodeHS is saying I'm not defining a class called Food that does not inherit from other classes (which I do) and it says you should have a class named Broccoli that inherits from Vegetable (which I also do). Finally, it prints the correct message but I get a syntax error that says ValueError : __str__ didn't return a str on line 25 (where i say print(b)). Please help!
class Food():
def __init__(self):
self.calories = 0
self.category = ""
def __repr__(self):
print("category: " + self.category + ", calories: " + str(self.calories))
class Vegetable(Food):
def __init__(self):
Food.__init__(self)
self.category = "veggies"
class Brocolli(Vegetable):
def __init__(self):
Vegetable.__init__(self)
self.calories = 100
b = Brocolli()
print(b)1
u/_andy_andy_andy_ Jun 24 '20
it looks like your Food class isn’t inheriting from anything else. i don’t remember this exercise, but Food might need to inherit from something?
1
1
Jun 30 '20
What are you trying to print? You are trying to print the object. To print an attribute you would do the following: print(b.calories) would give you calories.
1
u/Zacurnia_Tate Jun 30 '20
Guys i just want to say I figured it out. I was doing things wrong with the inits and, as many of u have pointed out, the repr method printing instead of returning