FAQ: Introduction to Functions - Review

This community-built FAQ covers the “Review” exercise from the lesson “Introduction to Functions”.

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

Computer Science
Data Science

FAQs on the exercise Review

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!

1 Like

12 posts were split to a new topic: Why does it print 10 times?

11 posts were split to a new topic: Questions about the specifics of strings

A post was split to a new topic: Why is this variable equal to a function?

Can someone take me through this program step by step?

1. def repeat_stuff(stuff, num_repeats=10):
2. return stuff*num_repeats

3. lyrics = repeat_stuff("Row ", 3) + "Your Boat. "
4. song = repeat_stuff(lyrics)

5. print(song)

-I understand that in line 1 we are defining a function with 2 arguments.

  • Line 2 we are returning the value so that we can use the arguments outside the function (maybe in it too?).
    -Line 3 is where you lose me. You are calling the function inside a variable using its arguments?
    -Line 4 I am lost. Youre calling the function again within a variable but instead of an argument you are calling the previous variable? Why do you not need 2 arguments since the functioned is defined with 2 to begin with?

i can highly recommend a tool to walk through your program like a debugger or:

http://www.pythontutor.com/visualize.html#mode=edit

we return a string. so for example:

repeat_stuff("Row ", 3)

then the function would return "Row Row Row".

we call the function with two arguments: "Row", which is a string, and 3 which is a integer.

the result of this function call is "Row Row Row" as we just determined. We then join/concat "Row Row Row" with "Your Boat"

so now we completed one lyric/line of our song.

variables are valid arguments, lyrics variable holds a string.

the second parameter has a default value (10), so when we don’t provide a second argument, the default is used.

so now we have a song which consist of 10 lines.

How do I know when to use + or comma to concatenate?
Does ‘cannot concatenate an integer’ apply to situations where either + or a comma is used?

Thanks.

Concatenation is a string and list operation, and it uses exclusively the + operator. The comma has only one purpose, to separate data.

When we print a comma separated list (a sequence) the commas don’t have any special meaning.

print ('one', 'two', 'three', 'four')

# one two three four

That is not concatenation. This is,

"The meaning of life is, " + 42 + "."

but as you’ve discovered this will raise an error since integers cannot be concatenated with strings unless they are first cast to a str object.

"The meaning of life is, " + str(42) + "."

Can someone explain this coding step by step? I am confused with the 10, 3, repeat_stuff(song) :disappointed_relieved:

There are explanations in this topic? Did you read them? Including a tool/website where you can run through your code step by step.

Why are you confused? If we understand the confusion, we can help better

I’m having trouble with step 3 for this one.
I don’t understand how to return the function.
I am doing this: return stuff*num_repeats
it just won’t work for me

We will need to see your code. Please post in a reply.

1 Like