Why does it print 10 times?

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?

15 Likes

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.

15 Likes

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.

7 Likes

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?

1 Like

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)
4 Likes

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.

2 Likes

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

1 Like

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.

1 Like

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.

2 Likes

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?

2 Likes

Hi,
Can someone explain to me the last part
def repeat_stuff(stuff, num_repeats = 10):
return (stuff*num_repeats)

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

song = repeat_stuff(lyrics)
print(song)

how come song = repeat_stuff(lyrics) repeats lyrics 10 times?
I mean I understand its because of this line of the code:
def repeat_stuff(stuff, num_repeats = 10):

But why it multiplied it 10 times? I mean the sign is = not *

the multiplying happens on the line below:

return (stuff*num_repeats)

num_repeats = 10 only means that the number of repeats will be 10 by default.

2 Likes

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.