Can I use a list as an attribute when creating a new object?
up vote
0
down vote
favorite
I've been learning python, and want to get started on my first project, ive finished learning about classes today, and would like to work on my understanding of algorithms, and how everything I've learned can tie together. I'd like to do this because I feel like these online resources give you good Info, but don't teach much about applying these concepts to projects.
I want to make a simple program, where I can type in a recipe name, and print the ingredients, cook time, steps, and name. I'd like to use a list for ingredients, and steps, and I'd like to print those in list format (perhaps wrapped in a border). Is this possible?
Class Recipe:
def __init__(self, recipe_name, ingredients, cook_time, steps)
(self.recipe_name = recipe_name)
(self.ingredients = ingredients)
(self.cook_time = cook_time)
(self.steps = steps)
Chicken Noodle = Recipe(Chicken Noodle, [Broth, noodles], 7 minutes, [Bring water to boil, add broth, etc.]
python
add a comment |
up vote
0
down vote
favorite
I've been learning python, and want to get started on my first project, ive finished learning about classes today, and would like to work on my understanding of algorithms, and how everything I've learned can tie together. I'd like to do this because I feel like these online resources give you good Info, but don't teach much about applying these concepts to projects.
I want to make a simple program, where I can type in a recipe name, and print the ingredients, cook time, steps, and name. I'd like to use a list for ingredients, and steps, and I'd like to print those in list format (perhaps wrapped in a border). Is this possible?
Class Recipe:
def __init__(self, recipe_name, ingredients, cook_time, steps)
(self.recipe_name = recipe_name)
(self.ingredients = ingredients)
(self.cook_time = cook_time)
(self.steps = steps)
Chicken Noodle = Recipe(Chicken Noodle, [Broth, noodles], 7 minutes, [Bring water to boil, add broth, etc.]
python
Thanks, how do I make sure it all ends up in the code box? I format while typing but then that happens.
– Adag89
Nov 11 at 2:57
Highlight the code you want to box up and hit ctrl+K
– Charles Landau
Nov 11 at 2:58
Perfect thanks.
– Adag89
Nov 11 at 2:58
You do not need any parentheses around the assignment statements, but you need a colon at the end of the second line.
– DYZ
Nov 11 at 3:14
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I've been learning python, and want to get started on my first project, ive finished learning about classes today, and would like to work on my understanding of algorithms, and how everything I've learned can tie together. I'd like to do this because I feel like these online resources give you good Info, but don't teach much about applying these concepts to projects.
I want to make a simple program, where I can type in a recipe name, and print the ingredients, cook time, steps, and name. I'd like to use a list for ingredients, and steps, and I'd like to print those in list format (perhaps wrapped in a border). Is this possible?
Class Recipe:
def __init__(self, recipe_name, ingredients, cook_time, steps)
(self.recipe_name = recipe_name)
(self.ingredients = ingredients)
(self.cook_time = cook_time)
(self.steps = steps)
Chicken Noodle = Recipe(Chicken Noodle, [Broth, noodles], 7 minutes, [Bring water to boil, add broth, etc.]
python
I've been learning python, and want to get started on my first project, ive finished learning about classes today, and would like to work on my understanding of algorithms, and how everything I've learned can tie together. I'd like to do this because I feel like these online resources give you good Info, but don't teach much about applying these concepts to projects.
I want to make a simple program, where I can type in a recipe name, and print the ingredients, cook time, steps, and name. I'd like to use a list for ingredients, and steps, and I'd like to print those in list format (perhaps wrapped in a border). Is this possible?
Class Recipe:
def __init__(self, recipe_name, ingredients, cook_time, steps)
(self.recipe_name = recipe_name)
(self.ingredients = ingredients)
(self.cook_time = cook_time)
(self.steps = steps)
Chicken Noodle = Recipe(Chicken Noodle, [Broth, noodles], 7 minutes, [Bring water to boil, add broth, etc.]
python
python
edited Nov 11 at 2:55
eyllanesc
68.6k93052
68.6k93052
asked Nov 11 at 2:53
Adag89
155
155
Thanks, how do I make sure it all ends up in the code box? I format while typing but then that happens.
– Adag89
Nov 11 at 2:57
Highlight the code you want to box up and hit ctrl+K
– Charles Landau
Nov 11 at 2:58
Perfect thanks.
– Adag89
Nov 11 at 2:58
You do not need any parentheses around the assignment statements, but you need a colon at the end of the second line.
– DYZ
Nov 11 at 3:14
add a comment |
Thanks, how do I make sure it all ends up in the code box? I format while typing but then that happens.
– Adag89
Nov 11 at 2:57
Highlight the code you want to box up and hit ctrl+K
– Charles Landau
Nov 11 at 2:58
Perfect thanks.
– Adag89
Nov 11 at 2:58
You do not need any parentheses around the assignment statements, but you need a colon at the end of the second line.
– DYZ
Nov 11 at 3:14
Thanks, how do I make sure it all ends up in the code box? I format while typing but then that happens.
– Adag89
Nov 11 at 2:57
Thanks, how do I make sure it all ends up in the code box? I format while typing but then that happens.
– Adag89
Nov 11 at 2:57
Highlight the code you want to box up and hit ctrl+K
– Charles Landau
Nov 11 at 2:58
Highlight the code you want to box up and hit ctrl+K
– Charles Landau
Nov 11 at 2:58
Perfect thanks.
– Adag89
Nov 11 at 2:58
Perfect thanks.
– Adag89
Nov 11 at 2:58
You do not need any parentheses around the assignment statements, but you need a colon at the end of the second line.
– DYZ
Nov 11 at 3:14
You do not need any parentheses around the assignment statements, but you need a colon at the end of the second line.
– DYZ
Nov 11 at 3:14
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Making a class to contain one recipe makes sense to me, but I would prefer a class to contain all my recipes:
class Recipes:
def __init__(self):
self.recipes = {}
def add_recipes(self, to_add):
for key in to_add:
self.recipes[key] = to_add[key]
def display_recipe(self, name):
recipe = self.recipes[name]
print("Name: ",name)
print("Ingredients: ", *recipe["ingredients"])
print("Cook time: ", recipe["cooktime"])
r = Recipes()
r.add_recipes({"Chicken Noodle": {"ingredients": ["Broth", "noodles"], "cooktime": "7 minutes"}})
r.display_recipe("Chicken Noodle")
You have some errors in your code:
Chicken Noodle = Recipe(Chicken Noodle, [Broth, noodles], 7 minutes, [Bring water to boil, add broth, etc.]
Needs to become:
ChickenNoodle = Recipe("Chicken Noodle", ["Broth", "noodles"], "7 minutes", ["Bring water to boil", "add broth, etc."])
The class definition also needs to change a bit, to conform to conventional style and some syntax rules:
class Recipe:
def __init__ (self, recipe_name, ingredients, cook_time, steps):
self.recipe_name = recipe_name
self.ingredients = ingredients
self.cook_time = cook_time
self.steps = steps
Could you elaborate on my syntax errors please? I keep getting syntax error when I try to run my code, and don't understand why, as it looks identical to every tutorial I've read in my eyes. I understand the need to put things in string format, I actually did that at first, but wasn't 100% sure so I took it out to see what happens.
– Adag89
Nov 11 at 3:43
When you get a syntax error it should point you to the line where the error is located @Adag89
– Charles Landau
Nov 11 at 3:49
I specifically don't understand what self.recipes{} does, and what r. Is. I've seen these explained a little in YouTube tutorials, but hasn't been touched on in sololearn.
– Adag89
Nov 11 at 3:51
It points me to: Class Recipe: with Recipe highlited red, but not explanation. I think I need to reinstall python, I get lots of weird errors I don't get using other computers.
– Adag89
Nov 11 at 3:52
r = Recipes()
instantiates the class, andself.recipes = {}
makes an empty dictionary that serves as a container where the class methods store and lookup recipes
– Charles Landau
Nov 11 at 3:54
|
show 1 more comment
up vote
0
down vote
I think you were pretty close! You don't need those parens in your constructor method. I removed those. To print out the entire recipe, we can simple use the to string function. Change it as you desire:
class Recipe:
def __init__(self, recipe_name, ingredients, cook_time, steps):
self.recipe_name = recipe_name
self.ingredients = ingredients
self.cook_time = cook_time
self.steps = steps
def __str__(self):
output = ''
output += 'Here is the recipe for {}:n'.format(self.recipe_name)
output += 'You will need: {}n'.format(self.ingredients)
output += 'This recipe takes: {}n'.format(self.cook_time)
output += 'Here are the steps involved:n'
for i, step in enumerate(self.steps):
output += 'Step {}: {}n'.format(i + 1, step)
return output
You can run this:
chicken_noodle = Recipe('Chicken Noodle', ['Broth', 'noodles'], '7 minutes', ['Bring water to boil', 'add broth'])
print (chicken_noodle)
output:
Here is the recipe for Chicken Noodle:
You will need: ['Broth', 'noodles']
This recipe takes: 7 minutes
Here are the steps involved:
Step 1: Bring water to boil
Step 2: add broth
Thank you, this is exactly what I had in mind.
– Adag89
Nov 11 at 4:03
When I execute this code I get an error that says that def str overrides def init method. Any idea why?
– Adag89
Nov 12 at 4:13
Hm, tough to tell. Are you running it with other code? Did you copy and past exactly as above and same indentation level?
– LeKhan9
Nov 12 at 4:27
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Making a class to contain one recipe makes sense to me, but I would prefer a class to contain all my recipes:
class Recipes:
def __init__(self):
self.recipes = {}
def add_recipes(self, to_add):
for key in to_add:
self.recipes[key] = to_add[key]
def display_recipe(self, name):
recipe = self.recipes[name]
print("Name: ",name)
print("Ingredients: ", *recipe["ingredients"])
print("Cook time: ", recipe["cooktime"])
r = Recipes()
r.add_recipes({"Chicken Noodle": {"ingredients": ["Broth", "noodles"], "cooktime": "7 minutes"}})
r.display_recipe("Chicken Noodle")
You have some errors in your code:
Chicken Noodle = Recipe(Chicken Noodle, [Broth, noodles], 7 minutes, [Bring water to boil, add broth, etc.]
Needs to become:
ChickenNoodle = Recipe("Chicken Noodle", ["Broth", "noodles"], "7 minutes", ["Bring water to boil", "add broth, etc."])
The class definition also needs to change a bit, to conform to conventional style and some syntax rules:
class Recipe:
def __init__ (self, recipe_name, ingredients, cook_time, steps):
self.recipe_name = recipe_name
self.ingredients = ingredients
self.cook_time = cook_time
self.steps = steps
Could you elaborate on my syntax errors please? I keep getting syntax error when I try to run my code, and don't understand why, as it looks identical to every tutorial I've read in my eyes. I understand the need to put things in string format, I actually did that at first, but wasn't 100% sure so I took it out to see what happens.
– Adag89
Nov 11 at 3:43
When you get a syntax error it should point you to the line where the error is located @Adag89
– Charles Landau
Nov 11 at 3:49
I specifically don't understand what self.recipes{} does, and what r. Is. I've seen these explained a little in YouTube tutorials, but hasn't been touched on in sololearn.
– Adag89
Nov 11 at 3:51
It points me to: Class Recipe: with Recipe highlited red, but not explanation. I think I need to reinstall python, I get lots of weird errors I don't get using other computers.
– Adag89
Nov 11 at 3:52
r = Recipes()
instantiates the class, andself.recipes = {}
makes an empty dictionary that serves as a container where the class methods store and lookup recipes
– Charles Landau
Nov 11 at 3:54
|
show 1 more comment
up vote
0
down vote
accepted
Making a class to contain one recipe makes sense to me, but I would prefer a class to contain all my recipes:
class Recipes:
def __init__(self):
self.recipes = {}
def add_recipes(self, to_add):
for key in to_add:
self.recipes[key] = to_add[key]
def display_recipe(self, name):
recipe = self.recipes[name]
print("Name: ",name)
print("Ingredients: ", *recipe["ingredients"])
print("Cook time: ", recipe["cooktime"])
r = Recipes()
r.add_recipes({"Chicken Noodle": {"ingredients": ["Broth", "noodles"], "cooktime": "7 minutes"}})
r.display_recipe("Chicken Noodle")
You have some errors in your code:
Chicken Noodle = Recipe(Chicken Noodle, [Broth, noodles], 7 minutes, [Bring water to boil, add broth, etc.]
Needs to become:
ChickenNoodle = Recipe("Chicken Noodle", ["Broth", "noodles"], "7 minutes", ["Bring water to boil", "add broth, etc."])
The class definition also needs to change a bit, to conform to conventional style and some syntax rules:
class Recipe:
def __init__ (self, recipe_name, ingredients, cook_time, steps):
self.recipe_name = recipe_name
self.ingredients = ingredients
self.cook_time = cook_time
self.steps = steps
Could you elaborate on my syntax errors please? I keep getting syntax error when I try to run my code, and don't understand why, as it looks identical to every tutorial I've read in my eyes. I understand the need to put things in string format, I actually did that at first, but wasn't 100% sure so I took it out to see what happens.
– Adag89
Nov 11 at 3:43
When you get a syntax error it should point you to the line where the error is located @Adag89
– Charles Landau
Nov 11 at 3:49
I specifically don't understand what self.recipes{} does, and what r. Is. I've seen these explained a little in YouTube tutorials, but hasn't been touched on in sololearn.
– Adag89
Nov 11 at 3:51
It points me to: Class Recipe: with Recipe highlited red, but not explanation. I think I need to reinstall python, I get lots of weird errors I don't get using other computers.
– Adag89
Nov 11 at 3:52
r = Recipes()
instantiates the class, andself.recipes = {}
makes an empty dictionary that serves as a container where the class methods store and lookup recipes
– Charles Landau
Nov 11 at 3:54
|
show 1 more comment
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Making a class to contain one recipe makes sense to me, but I would prefer a class to contain all my recipes:
class Recipes:
def __init__(self):
self.recipes = {}
def add_recipes(self, to_add):
for key in to_add:
self.recipes[key] = to_add[key]
def display_recipe(self, name):
recipe = self.recipes[name]
print("Name: ",name)
print("Ingredients: ", *recipe["ingredients"])
print("Cook time: ", recipe["cooktime"])
r = Recipes()
r.add_recipes({"Chicken Noodle": {"ingredients": ["Broth", "noodles"], "cooktime": "7 minutes"}})
r.display_recipe("Chicken Noodle")
You have some errors in your code:
Chicken Noodle = Recipe(Chicken Noodle, [Broth, noodles], 7 minutes, [Bring water to boil, add broth, etc.]
Needs to become:
ChickenNoodle = Recipe("Chicken Noodle", ["Broth", "noodles"], "7 minutes", ["Bring water to boil", "add broth, etc."])
The class definition also needs to change a bit, to conform to conventional style and some syntax rules:
class Recipe:
def __init__ (self, recipe_name, ingredients, cook_time, steps):
self.recipe_name = recipe_name
self.ingredients = ingredients
self.cook_time = cook_time
self.steps = steps
Making a class to contain one recipe makes sense to me, but I would prefer a class to contain all my recipes:
class Recipes:
def __init__(self):
self.recipes = {}
def add_recipes(self, to_add):
for key in to_add:
self.recipes[key] = to_add[key]
def display_recipe(self, name):
recipe = self.recipes[name]
print("Name: ",name)
print("Ingredients: ", *recipe["ingredients"])
print("Cook time: ", recipe["cooktime"])
r = Recipes()
r.add_recipes({"Chicken Noodle": {"ingredients": ["Broth", "noodles"], "cooktime": "7 minutes"}})
r.display_recipe("Chicken Noodle")
You have some errors in your code:
Chicken Noodle = Recipe(Chicken Noodle, [Broth, noodles], 7 minutes, [Bring water to boil, add broth, etc.]
Needs to become:
ChickenNoodle = Recipe("Chicken Noodle", ["Broth", "noodles"], "7 minutes", ["Bring water to boil", "add broth, etc."])
The class definition also needs to change a bit, to conform to conventional style and some syntax rules:
class Recipe:
def __init__ (self, recipe_name, ingredients, cook_time, steps):
self.recipe_name = recipe_name
self.ingredients = ingredients
self.cook_time = cook_time
self.steps = steps
answered Nov 11 at 3:11
Charles Landau
1,2191211
1,2191211
Could you elaborate on my syntax errors please? I keep getting syntax error when I try to run my code, and don't understand why, as it looks identical to every tutorial I've read in my eyes. I understand the need to put things in string format, I actually did that at first, but wasn't 100% sure so I took it out to see what happens.
– Adag89
Nov 11 at 3:43
When you get a syntax error it should point you to the line where the error is located @Adag89
– Charles Landau
Nov 11 at 3:49
I specifically don't understand what self.recipes{} does, and what r. Is. I've seen these explained a little in YouTube tutorials, but hasn't been touched on in sololearn.
– Adag89
Nov 11 at 3:51
It points me to: Class Recipe: with Recipe highlited red, but not explanation. I think I need to reinstall python, I get lots of weird errors I don't get using other computers.
– Adag89
Nov 11 at 3:52
r = Recipes()
instantiates the class, andself.recipes = {}
makes an empty dictionary that serves as a container where the class methods store and lookup recipes
– Charles Landau
Nov 11 at 3:54
|
show 1 more comment
Could you elaborate on my syntax errors please? I keep getting syntax error when I try to run my code, and don't understand why, as it looks identical to every tutorial I've read in my eyes. I understand the need to put things in string format, I actually did that at first, but wasn't 100% sure so I took it out to see what happens.
– Adag89
Nov 11 at 3:43
When you get a syntax error it should point you to the line where the error is located @Adag89
– Charles Landau
Nov 11 at 3:49
I specifically don't understand what self.recipes{} does, and what r. Is. I've seen these explained a little in YouTube tutorials, but hasn't been touched on in sololearn.
– Adag89
Nov 11 at 3:51
It points me to: Class Recipe: with Recipe highlited red, but not explanation. I think I need to reinstall python, I get lots of weird errors I don't get using other computers.
– Adag89
Nov 11 at 3:52
r = Recipes()
instantiates the class, andself.recipes = {}
makes an empty dictionary that serves as a container where the class methods store and lookup recipes
– Charles Landau
Nov 11 at 3:54
Could you elaborate on my syntax errors please? I keep getting syntax error when I try to run my code, and don't understand why, as it looks identical to every tutorial I've read in my eyes. I understand the need to put things in string format, I actually did that at first, but wasn't 100% sure so I took it out to see what happens.
– Adag89
Nov 11 at 3:43
Could you elaborate on my syntax errors please? I keep getting syntax error when I try to run my code, and don't understand why, as it looks identical to every tutorial I've read in my eyes. I understand the need to put things in string format, I actually did that at first, but wasn't 100% sure so I took it out to see what happens.
– Adag89
Nov 11 at 3:43
When you get a syntax error it should point you to the line where the error is located @Adag89
– Charles Landau
Nov 11 at 3:49
When you get a syntax error it should point you to the line where the error is located @Adag89
– Charles Landau
Nov 11 at 3:49
I specifically don't understand what self.recipes{} does, and what r. Is. I've seen these explained a little in YouTube tutorials, but hasn't been touched on in sololearn.
– Adag89
Nov 11 at 3:51
I specifically don't understand what self.recipes{} does, and what r. Is. I've seen these explained a little in YouTube tutorials, but hasn't been touched on in sololearn.
– Adag89
Nov 11 at 3:51
It points me to: Class Recipe: with Recipe highlited red, but not explanation. I think I need to reinstall python, I get lots of weird errors I don't get using other computers.
– Adag89
Nov 11 at 3:52
It points me to: Class Recipe: with Recipe highlited red, but not explanation. I think I need to reinstall python, I get lots of weird errors I don't get using other computers.
– Adag89
Nov 11 at 3:52
r = Recipes()
instantiates the class, and self.recipes = {}
makes an empty dictionary that serves as a container where the class methods store and lookup recipes– Charles Landau
Nov 11 at 3:54
r = Recipes()
instantiates the class, and self.recipes = {}
makes an empty dictionary that serves as a container where the class methods store and lookup recipes– Charles Landau
Nov 11 at 3:54
|
show 1 more comment
up vote
0
down vote
I think you were pretty close! You don't need those parens in your constructor method. I removed those. To print out the entire recipe, we can simple use the to string function. Change it as you desire:
class Recipe:
def __init__(self, recipe_name, ingredients, cook_time, steps):
self.recipe_name = recipe_name
self.ingredients = ingredients
self.cook_time = cook_time
self.steps = steps
def __str__(self):
output = ''
output += 'Here is the recipe for {}:n'.format(self.recipe_name)
output += 'You will need: {}n'.format(self.ingredients)
output += 'This recipe takes: {}n'.format(self.cook_time)
output += 'Here are the steps involved:n'
for i, step in enumerate(self.steps):
output += 'Step {}: {}n'.format(i + 1, step)
return output
You can run this:
chicken_noodle = Recipe('Chicken Noodle', ['Broth', 'noodles'], '7 minutes', ['Bring water to boil', 'add broth'])
print (chicken_noodle)
output:
Here is the recipe for Chicken Noodle:
You will need: ['Broth', 'noodles']
This recipe takes: 7 minutes
Here are the steps involved:
Step 1: Bring water to boil
Step 2: add broth
Thank you, this is exactly what I had in mind.
– Adag89
Nov 11 at 4:03
When I execute this code I get an error that says that def str overrides def init method. Any idea why?
– Adag89
Nov 12 at 4:13
Hm, tough to tell. Are you running it with other code? Did you copy and past exactly as above and same indentation level?
– LeKhan9
Nov 12 at 4:27
add a comment |
up vote
0
down vote
I think you were pretty close! You don't need those parens in your constructor method. I removed those. To print out the entire recipe, we can simple use the to string function. Change it as you desire:
class Recipe:
def __init__(self, recipe_name, ingredients, cook_time, steps):
self.recipe_name = recipe_name
self.ingredients = ingredients
self.cook_time = cook_time
self.steps = steps
def __str__(self):
output = ''
output += 'Here is the recipe for {}:n'.format(self.recipe_name)
output += 'You will need: {}n'.format(self.ingredients)
output += 'This recipe takes: {}n'.format(self.cook_time)
output += 'Here are the steps involved:n'
for i, step in enumerate(self.steps):
output += 'Step {}: {}n'.format(i + 1, step)
return output
You can run this:
chicken_noodle = Recipe('Chicken Noodle', ['Broth', 'noodles'], '7 minutes', ['Bring water to boil', 'add broth'])
print (chicken_noodle)
output:
Here is the recipe for Chicken Noodle:
You will need: ['Broth', 'noodles']
This recipe takes: 7 minutes
Here are the steps involved:
Step 1: Bring water to boil
Step 2: add broth
Thank you, this is exactly what I had in mind.
– Adag89
Nov 11 at 4:03
When I execute this code I get an error that says that def str overrides def init method. Any idea why?
– Adag89
Nov 12 at 4:13
Hm, tough to tell. Are you running it with other code? Did you copy and past exactly as above and same indentation level?
– LeKhan9
Nov 12 at 4:27
add a comment |
up vote
0
down vote
up vote
0
down vote
I think you were pretty close! You don't need those parens in your constructor method. I removed those. To print out the entire recipe, we can simple use the to string function. Change it as you desire:
class Recipe:
def __init__(self, recipe_name, ingredients, cook_time, steps):
self.recipe_name = recipe_name
self.ingredients = ingredients
self.cook_time = cook_time
self.steps = steps
def __str__(self):
output = ''
output += 'Here is the recipe for {}:n'.format(self.recipe_name)
output += 'You will need: {}n'.format(self.ingredients)
output += 'This recipe takes: {}n'.format(self.cook_time)
output += 'Here are the steps involved:n'
for i, step in enumerate(self.steps):
output += 'Step {}: {}n'.format(i + 1, step)
return output
You can run this:
chicken_noodle = Recipe('Chicken Noodle', ['Broth', 'noodles'], '7 minutes', ['Bring water to boil', 'add broth'])
print (chicken_noodle)
output:
Here is the recipe for Chicken Noodle:
You will need: ['Broth', 'noodles']
This recipe takes: 7 minutes
Here are the steps involved:
Step 1: Bring water to boil
Step 2: add broth
I think you were pretty close! You don't need those parens in your constructor method. I removed those. To print out the entire recipe, we can simple use the to string function. Change it as you desire:
class Recipe:
def __init__(self, recipe_name, ingredients, cook_time, steps):
self.recipe_name = recipe_name
self.ingredients = ingredients
self.cook_time = cook_time
self.steps = steps
def __str__(self):
output = ''
output += 'Here is the recipe for {}:n'.format(self.recipe_name)
output += 'You will need: {}n'.format(self.ingredients)
output += 'This recipe takes: {}n'.format(self.cook_time)
output += 'Here are the steps involved:n'
for i, step in enumerate(self.steps):
output += 'Step {}: {}n'.format(i + 1, step)
return output
You can run this:
chicken_noodle = Recipe('Chicken Noodle', ['Broth', 'noodles'], '7 minutes', ['Bring water to boil', 'add broth'])
print (chicken_noodle)
output:
Here is the recipe for Chicken Noodle:
You will need: ['Broth', 'noodles']
This recipe takes: 7 minutes
Here are the steps involved:
Step 1: Bring water to boil
Step 2: add broth
answered Nov 11 at 3:14
LeKhan9
911111
911111
Thank you, this is exactly what I had in mind.
– Adag89
Nov 11 at 4:03
When I execute this code I get an error that says that def str overrides def init method. Any idea why?
– Adag89
Nov 12 at 4:13
Hm, tough to tell. Are you running it with other code? Did you copy and past exactly as above and same indentation level?
– LeKhan9
Nov 12 at 4:27
add a comment |
Thank you, this is exactly what I had in mind.
– Adag89
Nov 11 at 4:03
When I execute this code I get an error that says that def str overrides def init method. Any idea why?
– Adag89
Nov 12 at 4:13
Hm, tough to tell. Are you running it with other code? Did you copy and past exactly as above and same indentation level?
– LeKhan9
Nov 12 at 4:27
Thank you, this is exactly what I had in mind.
– Adag89
Nov 11 at 4:03
Thank you, this is exactly what I had in mind.
– Adag89
Nov 11 at 4:03
When I execute this code I get an error that says that def str overrides def init method. Any idea why?
– Adag89
Nov 12 at 4:13
When I execute this code I get an error that says that def str overrides def init method. Any idea why?
– Adag89
Nov 12 at 4:13
Hm, tough to tell. Are you running it with other code? Did you copy and past exactly as above and same indentation level?
– LeKhan9
Nov 12 at 4:27
Hm, tough to tell. Are you running it with other code? Did you copy and past exactly as above and same indentation level?
– LeKhan9
Nov 12 at 4:27
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245446%2fcan-i-use-a-list-as-an-attribute-when-creating-a-new-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Thanks, how do I make sure it all ends up in the code box? I format while typing but then that happens.
– Adag89
Nov 11 at 2:57
Highlight the code you want to box up and hit ctrl+K
– Charles Landau
Nov 11 at 2:58
Perfect thanks.
– Adag89
Nov 11 at 2:58
You do not need any parentheses around the assignment statements, but you need a colon at the end of the second line.
– DYZ
Nov 11 at 3:14