FAQ: Survey of Computer Science - The Tale of Kenny - Iteration

Community%20FAQs%20on%20Codecademy%20Exercises

This community-built FAQ covers the “Iteration” exercise from the lesson “Survey of Computer Science - The Tale of Kenny”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Code Foundations

FAQs on the exercise Iteration

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

what does the (being_cooked) represent in this code?

1 Like

If I understand correctly “being_cooked” is used as part of the definition of the function to stand for whatever the function will enter into the code. In this case, the list “on_the_griddle” (which stands for Kenny’s four sandwiches) is being plugged in to the space for “being_cooked”, and by doing that, that item is being added to each of the sandwiches in the list (like py add_spinach(on_the_griddle) adds spinach to the sandwiches).

1 Like

Pasting the line “py add_spinach(on_the_griddle)” as instructed doesn’t work and returns a syntax error. It works fine without the “py”

3 Likes

That instruction may need review.

 py add_cheese(on_the_griddle)

is something we would write into the command line (bash), not the code source. This lesson does not have a command line.

2 Likes

I couldn’t Really see what I was supposed to end up with, even though it let me move on. I had lots of iterations of eggs spinach etc, even though i was supposed ot have 4 omlets Very unclear instructions, it literally tells you to paste incorrect syntax

1 Like

Hi, I think the cause of the confusion is that it printed out the entire list every time you added an ingredient (called a function). In a real application much of this would be done under the hood but I think they are trying to demonstrate the basics of how lists work on a fundamental level.

Your output, three lists, was probably confusing because it looked like this :

[[‘eggs’, ‘spinach’], [‘eggs’, ‘spinach’], [‘eggs’, ‘spinach’], [‘eggs’, ‘spinach’]]
[[‘eggs’, ‘spinach’, ‘mushrooms’], [‘eggs’, ‘spinach’, ‘mushrooms’], [‘eggs’, ‘spinach’, ‘mushrooms’], [‘eggs’, ‘spinach’, ‘mushrooms’]]
[[‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’], [‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’], [‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’], [‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’]]

The reason the output looks like this in this example is because every time you called a function it iterated through the list and performed the algorithm in the function you called including the output. When you ran all three you got the output of all three.

So when you ran:

add_spinach(on_the_griddle)

You called the add_spinach function and used the list “on_the_griddle” as an argument. Since your argument is a list, the function iterated through every item on the list and added spinach, giving you an output of the entire list:

[[‘eggs’, ‘spinach’], [‘eggs’, ‘spinach’], [‘eggs’, ‘spinach’], [‘eggs’, ‘spinach’]]

When you called add_mushrooms, it did the exact same thing, iterating through the list and adding the sting ‘mushrooms’, giving you the output of the entire list:

[[‘eggs’, ‘spinach’, ‘mushrooms’], [‘eggs’, ‘spinach’, ‘mushrooms’], [‘eggs’, ‘spinach’, ‘mushrooms’], [‘eggs’, ‘spinach’, ‘mushrooms’]]

Finally you added cheese by calling add_cheese and now you have the output of the list with all ingredients.

[[‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’], [‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’], [‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’], [‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’]]

The point of the application was to generate this list. In a real world applications adding cheese or spinach would be given as a choice, you would add all the ingredients you needed and get the final output, and though the lists would be changed you would only see the final product, which in this case is:

[[‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’], [‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’], [‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’], [‘eggs’, ‘spinach’, ‘mushrooms’, ‘cheese’]]

The other printed lists are from the two previous functions you called and ran. Normally , even if those lists were changed, you would not see them printed. Here they were printed to show what is happening in the background.

As a side note, adding print statements to functions, even when they are not needed, helps you debug or just better understand what your code is doing at every step, as it did in this one.

I know this was a long post, I promise these concepts are important even if they seem basic. I hope it helped!

6 Likes

I have copied / pasted the code into the editor, but I’m getting a syntax error (invalid syntax) - is this a known error, or am I doing something wrong? Thanks for any help you can offer!

the lesson has two errors. they display

py add_cheese(on_the_griddle)

when it should really just be

“add_cheese(on_the_griddle)”

that you are pasting into the editor.

3 Likes

corect code ison_the_griddle = [[“eggs”], [“eggs”], [“eggs”], [“eggs”]]

def add_spinach(being_cooked):
for item in being_cooked:
item.append(“spinach”)
print(being_cooked)
def add_mushrooms(being_cooked):
for item in being_cooked:
item.append(“mushrooms”)
print(being_cooked)
add_mushrooms(on_the_griddle)
def add_cheese(being_cooked):
for item in being_cooked:
item.append(“cheese”)
print(being_cooked)

Paste your code on the lines below

The calls to your functions appear to be missing. Call each one with on_the_griddle as the argument.

Thanks for your help :slight_smile:

Hi. I can’t copy and paste. I can copy but the item will not paste. I understand the exercise, I just typed in the commands one at a time and completed the exercise correctly. Please advise.

Hi, I tried to copy and paste but it didn’t work. I had to manually type the info on the quizzes. Anyone else have this problem?

on_the_griddle = [[“eggs”], [“eggs”], [“eggs”], [“eggs”]]

def add_spinach(being_cooked):
for item in being_cooked:
item.append(“spinach”)
print(being_cooked)

def add_mushrooms(being_cooked):
for item in being_cooked:
item.append(“mushrooms”)
print(being_cooked)

def add_cheese(being_cooked):
for item in being_cooked:
item.append(“cheese”)
print(being_cooked)

Paste your code on the lines below:

add_spinach(on_the_griddle)
add_mushrooms(on_the_griddle)
add_cheese(on_the_griddle)

Is this the right answer or I messed up somewhere ? Thanks for the input.