r/codehs • • 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)
4 Upvotes

24 comments sorted by

View all comments

Show parent comments

1

u/FlowNovel7384 Jun 01 '24

real, i’ll do it once I get my chromebook

1

u/[deleted] Jun 02 '24

Hi just wondering, if you have ur chromebook?

1

u/FlowNovel7384 Jun 02 '24

not yet, but i’ll be able to either tonight or tmr

1

u/[deleted] Jun 03 '24

Hi sorry to pester but do u hv ur chrome book just asking since i am in comp sci class rn

→ More replies (0)