fruit practice problems for python
#Class Challenge Problems
class Fruit:
calories = 30
sugar = 3
def get_calories(self):
return self.calories
class Apple(Fruit):
def __init__(self):
self.carbs = 5
self.protein = 2
def __repr__(self):
return "Carbs:{0}, Protein:{1}".format(self.carbs, self.protein)
def use_protein(self):
if self.protein == 0:
return 4
self.protein -= 1
return 1
class RedApple(Apple):
color = "red"
calories = 20
def __repr__(self):
return super(RedApple, self).__repr__() + " Color:" + self.color
#Problem 1
#What will the following statements print?
food = RedApple()
print(food)
print(food.calories)
food.use_protein()
print(food.protein)