Learn Python: Functions - Lesson 11, Review


#1

First I think it’s important to make a distinction: the FAQs I found in the forum(or google) all referred to the Learn Python course, where the Functions chapter has 17 lessons. I’m doing the “CS Path Intro”, whose Python Functions area has 11 lessons. The final one is a review, and this is where my doubt stems from.
First you’re asked to define a function, ok no problem with that. In step two, however, I’m having an issue, it says:
" Outside of the function, call repeat_stuff .
You can use the value "Row " for stuff and 3 for num_repeats ."

At the end of the first step, my code looked like:

def repeat_stuff(stuff, num_repeats):
      print()

because it explicitly asked to write an empty print statement, the problem comes when it’s time to fill in the blanks in step two(which I copied above, the “Outside of the function[…]” part), where I wrote the following code:

def repeat_stuff(stuff, num_repeats):
     return stuff*num_repeats
repeat_stuff("Row", 3)

I thought the question was quite straigthforward and this would do it, but the tutorial header in the bottom says: " Expected "Row " to be passed into the function repeat_stuff"
After pondering about it for a while I couldn’t get what the exercise wanted out of me after all, so I asked for the solution. The problem is that it solves the entire lesson, which doesn’t answer what I should do at this particular step of the exercise. It adds other variables mentioned later on, but that’s not part of this stage and doesn’t really answer what I should do at this point.

If anyone can help me with this I greatly appreciate it.


#2

It will be simpler for us to address this question if we have a link to the exercise. Please copy and paste the URL in the location bar of the lesson page.


#3

My bad, I copied the link before, I don’t know why I didn’t post it. Anyway, here it is:
https://www.codecademy.com/paths/computer-science/tracks/cspath-intro/modules/cspath-python-functions/lessons/intro-to-functions/exercises/review


#4

Have you got a checkmark for step 2?


#5

No, that’s where my doubt comes from. I got a check in step 1, where it asks only to define the function and add an empty print statement. In step 2 I did as described in the OP, got a “X” mark and the mentioned “Expected ‘Row’ to be passed into the function repeat_stuff’” observation at the bottom


#6

I don’t see any fault in your code. Perhaps try a refresh and Run again to see what happens. Failing that, Reset the exercise and repeat the steps (no pun intended).


#7

I have done that several times already, I’ve even waited a day to see if anything changed. I don’t know why all forum posts regarding Python functions seem to be from that other course either.
I didn’t quite get what it was meant with the “Expected ‘Row’[…]” statement though, maybe there lies the answer. Saying “to be passed into the function” in this context sounds ambiguous to me.


#8

This is synonymous with saying, ‘call the function with the arguments, “Row” and 3.’


#9

That’s what I had thought at first, but I was sure it was supposed to mean something else since, well, I DID call the function with those exact same arguments. So it seems I’m in the right here and this is an error in the lesson itself?
I’ll just go on with the lessons and hope to consolidate the knowledge with other sources if that’s the case.


#10

Can I show you the solution that is saved in my profile?


#11

It says which value it expected and you’re using another one. No mystery here.
Keep in mind that similar doesn’t mean same. It takes a different kind of comparison here where any difference means it’s different.


#12

Please do, this is puzzling me. Like I said before, the solution embedded in the lesson itself does the other steps too so it can get confusing,


#13

How so? It asked for the “Row” value to be called in the function, and that’s exactly what I did.


#14

The proviso of course is that this bypasses the step 2 problem you are experiencing. When I did this some time ago everything went fine. Unless there is a huge d’oh! in there that I am blind to, there is no explanation why your arguments are not being seen by the SCT.

This will let you look at it…

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

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

print(song)

This may at least let you complete the exercise without getting the solution. Can’t promise anything, mind.

Consider removing parts of the solution above if the next lesson continues from this one. Keep only the parts that should be there to start with.


#15

That is nearly the same as the lesson’s solution, apart from the “\n”, but then at least I’m not crazy to think my original code was correct, knowing how it is(or at least how it’s supposed to be) is what matters.
Thanks a lot for your help, and especially for replying to the thread right away.


#16

They’re both very short, you don’t need me to point out the difference.
You need to compare them, this is not difficult, but you do need to stop taking “looks the same” as “they are equal”

There are many ways you can compare. Eyeballing is one of them, it is very much visible to the eye, the problem is in removing the difference during your comparison. Another is to paste them into python and use == to see that, yes, they are indeed different, and having them so close to each other may also make it easier to compare them by eyeball, which brings us to next comparison methods which is pasting them both in to a text editor one under the other. Or for that matter, convert the strings into lists of bytes, there’s a chance you’re not seeing the strings for what they are and a list of bytes would give you a number for each character which is completely unambiguous.

>>> list('some text'.encode())
[115, 111, 109, 101, 32, 116, 101, 120, 116]

Or in python2:

>>> list(bytearray('some text'))
[115, 111, 109, 101, 32, 116, 101, 120, 116]

#17

Are you referring to the two pieces of code I posted? Each of them is a different step, I know they are different. Like I said, the first step asked to add only an empty print statement to the function, it was in step two that it was supposed to return something. My doubt arose after not getting any results despite the seemingly correct code, which was confirmed by mtf. The tutorial “troubleshooting bar” in the bottom claimed I hadn’t used “Row” when calling the function, which I did. Here’s the link again if it’s useful:

https://www.codecademy.com/paths/computer-science/tracks/cspath-intro/modules/cspath-python-functions/lessons/intro-to-functions/exercises/review

And if it’s of any importance, “list” and “encode()” haven’t been covered in the lessons so far.


#18

Compare the argument in the example last given to the argument in your example. See the difference? In my blindness yesterday it escaped me, but jumped right out immediately, today.


#19

When you say the last given example you mean when you posted your own solution, correct? Because what ionatan posted(>>>list(‘some text’ .encode()) etc.) is something else entirely.

If you’re indeed referring to your answer, the only differences in the function’s arguments are num_repeats = 10 and the creation of the variables lyrics and song, but I didn’t do that in the code I originally posted because that’s only asked later, in step 2 all it says is the function should return stuff*num_repeats and use “Row” and 3 as parameters when calling it. These are the steps:
1.

Define a function called repeat_stuff that takes in two inputs, stuff , and num_repeats .

We will want to make this function print a string with stuff repeated num_repeats amount of times. For now, only put an empty print statement inside the function.

I got a checkmark on that, no problem. Step 2:

2.

Outside of the function, call repeat_stuff .

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

So what I did at this point was go back to the function, delete the empty print statement, add return stuff * num_repeats and then call the function with “Row” and 3, as in repeat_stuff(“Row”, 3). The tutorial only asks to substitute the print() for return on step 3, but calling repeat_stuff with only a print() in its definition would result in nothing, although I tried it for the sake of the exercise, thinking it would go along the lines of “See? You need a return for the function to do anything, now go ahead and add it”. If I took the descriptions literally, by step 2 the code would look like this:
def repeat_stuff(stuff, num_repeats):
print()
repeat_stuff(“Row”, 3)
This was signaled as incorrect and brought up that same remark “Expected ‘Row’ to be passed into the function repeat_stuff”. If neither taking the directions literally nor trying to anticipate the next steps worked, then I don’t know what will.

I’m obviously confusing something here, I just fail to see what.


#20

Now compare to yours. As mentioned yesterday, it was probably one of those d'oh! moments for me.