Store a list of strings in one attribute
Question:
I think I might not have explained my question clearly. I apologise for that. I will try again.
I have a parent class with certain attributes:
class Restaurant():
'This is the restaurant class'
def __init__(self, name, cuisine_type):
self.name = name
self.cuisine_type = cuisine_type
Then I have a child class inheriting all the parent’s attributes and adding a new one:
class IceCreamStand():
def __init__(self, *flavor):
'Attributes of parent class initialised'
self.flavor = flavor
Now I try printing a list of of flavours stored in the attribute flavor:
def desc_flavor(self):
print('This Ice_Cream shop has ' + self.flavor + ' flavors')
flavor1 = IceCreamStand('Mango', 'Raspberry', 'Coffee', 'Vanilla')
If I use concat I get the message saying that the name is not defined.
My apologies for not explaining the problem correctly the first time and thanks for all the help.
Answers:
Try using the below code:
def __init__(self, *attribute1):
self.atributte1 = attribute1
Use Arbitrary argument list. See this answer.
Example:
li = []
def example(*arg):
li = list(arg)
print(li)
example('string1', 'string2', 'string3')
class IceCreamStand(Restaurant):
def __init__(self,restaurant_name, cuisine_type):
super().__init__(restaurant_name, cuisine_type)
def describe_flavors(self,*flavors):
print(f'{self.restaurant_name} has the following flavors:')
for self.flavor in flavors:
print(f'-{self.flavor}')
restaurant =IceCreamStand('DQ','ice cream')
restaurant.describe_restaurant()
restaurant.describe_flavors('Chocolate','Mango', 'Raspberry', 'Coffee', 'Vanilla')
As far I know you are doing exercise of chapter 9 from the book of Python Crash Course. This is the code which I did as its part of another exercise. Hope this helps you.
class Restaurant():
"""A simple attempt to model a restaurant."""
def __init__(self, restaurant_name, cusisine_type):
self.name = restaurant_name
self.type = cusisine_type
def describe_restaurant(self):
print("Restaurant name is " + self.name.title() + ".")
# print(self.name.title() + " is a " + self.type + " type restaurant.")
def open_restaurant(self):
print(self.name.title() + " is open!")
class IceCreamStand(Restaurant):
"""Making a class that inherits from Restaurant parent class."""
def __init__(self, restaurant_name, cusisine_type):
super().__init__(restaurant_name, cusisine_type)
self.flavor = 'chocolate'
def display_flavors(self):
print("This icrecream shop has " + self.flavor + " flavor.")
# create an instance of IceCreamStand
falvors = IceCreamStand('baskin robbins', 'icecream')
# print("My restaurant name is: " + falvors.name)
# print("Restaurant is which type: " + falvors.type)
falvors.describe_restaurant()
falvors.open_restaurant()
# Calling this method
falvors.display_flavors()
I think I might not have explained my question clearly. I apologise for that. I will try again.
I have a parent class with certain attributes:
class Restaurant():
'This is the restaurant class'
def __init__(self, name, cuisine_type):
self.name = name
self.cuisine_type = cuisine_type
Then I have a child class inheriting all the parent’s attributes and adding a new one:
class IceCreamStand():
def __init__(self, *flavor):
'Attributes of parent class initialised'
self.flavor = flavor
Now I try printing a list of of flavours stored in the attribute flavor:
def desc_flavor(self):
print('This Ice_Cream shop has ' + self.flavor + ' flavors')
flavor1 = IceCreamStand('Mango', 'Raspberry', 'Coffee', 'Vanilla')
If I use concat I get the message saying that the name is not defined.
My apologies for not explaining the problem correctly the first time and thanks for all the help.
Try using the below code:
def __init__(self, *attribute1):
self.atributte1 = attribute1
Use Arbitrary argument list. See this answer.
Example:
li = []
def example(*arg):
li = list(arg)
print(li)
example('string1', 'string2', 'string3')
class IceCreamStand(Restaurant):
def __init__(self,restaurant_name, cuisine_type):
super().__init__(restaurant_name, cuisine_type)
def describe_flavors(self,*flavors):
print(f'{self.restaurant_name} has the following flavors:')
for self.flavor in flavors:
print(f'-{self.flavor}')
restaurant =IceCreamStand('DQ','ice cream')
restaurant.describe_restaurant()
restaurant.describe_flavors('Chocolate','Mango', 'Raspberry', 'Coffee', 'Vanilla')
As far I know you are doing exercise of chapter 9 from the book of Python Crash Course. This is the code which I did as its part of another exercise. Hope this helps you.
class Restaurant():
"""A simple attempt to model a restaurant."""
def __init__(self, restaurant_name, cusisine_type):
self.name = restaurant_name
self.type = cusisine_type
def describe_restaurant(self):
print("Restaurant name is " + self.name.title() + ".")
# print(self.name.title() + " is a " + self.type + " type restaurant.")
def open_restaurant(self):
print(self.name.title() + " is open!")
class IceCreamStand(Restaurant):
"""Making a class that inherits from Restaurant parent class."""
def __init__(self, restaurant_name, cusisine_type):
super().__init__(restaurant_name, cusisine_type)
self.flavor = 'chocolate'
def display_flavors(self):
print("This icrecream shop has " + self.flavor + " flavor.")
# create an instance of IceCreamStand
falvors = IceCreamStand('baskin robbins', 'icecream')
# print("My restaurant name is: " + falvors.name)
# print("Restaurant is which type: " + falvors.type)
falvors.describe_restaurant()
falvors.open_restaurant()
# Calling this method
falvors.display_flavors()