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.]









share|improve this question
























  • 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















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.]









share|improve this question
























  • 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













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.]









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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





share|improve this answer





















  • 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, 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


















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





share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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





share|improve this answer





















  • 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, 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















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





share|improve this answer





















  • 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, 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













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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










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, 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


















  • 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, 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
















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












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





share|improve this answer





















  • 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















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





share|improve this answer





















  • 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













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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python