FAQ: Introduction to Functions - Review


#1

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!


#2

Hi there, quick question!

I understand that with the ‘lyrics’ variable we over-road the number 10 in the function and therefore “Row” appeared 3 times. I’m just wondering what part of the code told “print” to display the whole function 10 times?


#3

This
lyrics = repeat_stuff("Row ", 3) + "Your Boat. "
runs the function giving two arguments, therefore overwriting the 10.

The second step
song = repeat_stuff(lyrics)
runs the function again but this time you’re not overwriting the default num_repeats argument of 10 since you’re not giving a second argument.


#4

I think that when we put (“row”, 3) it defines num_repeats as 3, but when we put just (lyrics) with out any specified number, it used the defined default of 10 applied in step four. I think.


#5

Hi guys,

I’m currently stuck at sub exercise 2. which states

Outside of the function, call repeat_stuff .

You can use the value "Row " for stuff and 3 for num_repeats .

what does that translate into python ?

I can’t seem to get it to greenlight it to move to step 3.


#7

Make sure you put the space behind “Row” so that it is "Row ".


#8

Make sure your code is not indented two spaces for this line:

repeat_stuff("Row ", 3)


#9

Hey, thanks for the help!


#10

Why is it that when you define the variable lyrics without the 10 being default it won’t run even though in lyrics we defined both positional arguments?


#11

num_repeats will be 10 if we do not supply a second positional argument.

lyrics = repeat_stuff("Row ") + "Your Boat\n"
Row Row Row Row Row Row Row Row Row Row Your Boat

If we supply no arguments it will raise an exception.

TypeError: repeat_stuff() missing 1 required positional argument: 'stuff'

If we supply a number in the first position, it will also raise an exception:

TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

Note that we are building lyrics before passing it to create song.

Row Row Row your boat

is the lyrics,

Row Row Row Your Boat
Row Row Row Your Boat
Row Row Row Your Boat
Row Row Row Your Boat
Row Row Row Your Boat
Row Row Row Your Boat
Row Row Row Your Boat
Row Row Row Your Boat
Row Row Row Your Boat
Row Row Row Your Boat

is the song, as in,

song = repeat_stuff(lyrics)

#12

You’re right. To reiterate this,

First when the function repeat_stuff is called, the num_repeats is overwritten by 3 specified in the argument. So it print Row Row Row your boat.

Now the next line is executed song=repeat_stuff(lyrics). Note that argument is only one. That argument is assigned to stuff. Argument now holds this value “Row Row Row your boat.” and since the num_repeats is 10. It prints the whole string 10 times.


#13

I’m having a hard time figuring out I am doing wrong here.

Number 5


#14

Great question! This thread helped me too because I thought the same thing! Got it now!!


#17

Could please explain this again. I am unable to understand why the code will show error if num_repeats = 10 is not mentioned. Thank you.


#18

Because you are only using:

song = repeat_stuff(lyrics)

But the function repeat_stuff needs two parameters to run:

def repeat_stuff(stuff, num_repeats=10):

So, when you are using:

song = repeat_stuff(lyrics)

and num_repeats = 10 is not mentioned.

The num_repeats is missing and that value is needed.


#19

the idea is that if the second argument is missing, the parameter uses the default value. 10 in this case

@aayushishah525, can you post the code with the error so we can better understand what you are talking about?