![]() |
Child Class, Loop list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Child Class, Loop list (/thread-20567.html) |
Child Class, Loop list - bjornnotborg - Aug-19-2019 Greetings! I've been struggling with printing my list elements, called from a method. Have been trying to approach this from all kinds of angles, but got stuck on each attempt. Here is my code: class Restaurant(): """A simple attempt to model a restaurant""" def __init__(self, restaurant_name, cuisine_type): """Initialize name and cuisine attributes""" self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type def describe_restaurant(self): """Print restaurant name and cuisine type""" print("Welcome to " + self.restaurant_name.title() + ".") print("We serve a healthy variety of dishes to suit your palette, most of our food is based on " + self.cuisine_type + ".") def open_restaurant(self): """Display a message that the restaurant is open""" print(self.restaurant_name.title() + " is open!") class IceCreamStand(Restaurant): """Represents aspects of a restaurant, specific to an Ice-cream stand.""" def __init__(self, flavors, restaurant_name, cuisine_type): """Initialize attributes to describe flavors in the Ice-cream stand""" super().__init__(flavors, restaurant_name, cuisine_type) self.flavors = ['lemon', 'malaga', 'pistacio', 'mocca'] self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type #This last part seems to be the issue: def flavors(self, flav): for flavors in self.flavors: print(flavor) self.flav = ice_stand.flavors() ice_stand = IceCreamStand(self.flav, 'ice', 'ok')Any help is appreciated! RE: Child Class, Loop list - Gribouillis - Aug-19-2019 You need to choose different names for the attribute self.flavors and the method def flavors(self) otherwise the expression self.flavors will always retrieve the attribute instead of the method. I suggest def print_flavors() as it better describes what the method actually does.
RE: Child Class, Loop list - bjornnotborg - Aug-28-2019 That, and some other changes seemed to have fixed it, thank you! |