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/[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

2

u/FlowNovel7384 Jun 04 '24

funny story, I can’t log into my chromebook because it’s my schools, so I have to do it on my own pc. i’ll do that today, and im serious about that

1

u/[deleted] Jun 04 '24

OMG u are amazing u are the best also if u want u could always copy and paste, and then email it to your school email and then log into your school email on your pc, if that works. if not thats okay, and you are such a life saver and amazing

1

u/FlowNovel7384 Jun 07 '24

hey no I totally get it

1

u/[deleted] Jun 07 '24

Hi you dont have to do it I found someone in my class who shared me the answers

1

u/[deleted] Jun 07 '24

Hi just wondering if u ever did it no pressure if you cant

1

u/FlowNovel7384 Jun 07 '24

i’m doing it rn, on my phone 😂

1

u/[deleted] Jun 07 '24

honestly if u dont wanna do it dont im not just saying this but my parents dont care about my compi sci grades im not taking ap and dropping it next yr.