Questions about the specifics of strings

here is what worked for me:

def repeat_stuff(stuff, num_repeats=10):

return stuff*num_repeats

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

song = repeat_stuff(lyrics)

print(song)

hi, can some one please explain for this particular exercise, How exactly we got the output we did?
I mean, what made Row"" to print 3 times, and over all out put to print 10 times?
I can see for default we had " num_repeats"= 10, and then we did stuff* num_repeats, that resulted in 10 prints, but how and where exactly we got 3 “Rows” from in the out put??

new to coding!!!

this is the output I got:

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.

It depends what steps you took to get there. Did you perhaps use the repeat_stuff function on the string 'Row'?

==============================================

def repeat_stuff (stuff, num_repeats):

return stuff*num_repeats

repeat_stuff("Row ",3)

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

song = repeat_stuff (lyrics, 1)

print (song)

My logic

So, I finally did this and got the result and I am not sure if what I did was right or wrong. Please help.
As per the exercise, I had initially made num_repeats = 10, but when calling it finally, I changed it to 1.

So, stuff = Row, num_repeats = 3
Calling the function with the arguments Row, 3 i.e., repeat_stuff("Row ",3) gave me Row Row Row
Then, I went ahead and concatenated "Row Row Row " with “Your Boat” and stored it in lyrics
So, basically, lyrics = Row Row Row Your Boat

Then song = repeat_stuff (stuff, num_repeats) = repeat_stuff (lyrics, 1)
So Row Row Row Your Boat was printed 1 time.

Please let me know if I am wrong. I am super new to this and really appreciate your help.

this is for the project- getting ready for physics class

7 assignment

my input-

print(“The GE train supplies " + str(train_force) + " Newtons of force.”)

OUT put

File “script.py”, line 18
print(“The GE train supplies " + str(train_force) + " Newtons of force.”)
^
SyntaxError: invalid character in identifier

Question -

can some one please explain why I am getting this error? it seems right to me, and similar thing was done on the video by the instructor for this project

Hello @suran6jana and welcome to the Codecademy Forums!

Please format your code using the </> button. It looks like you call repeat_stuff() on line 3 and again on line 4. However, only the call on line 4 is used. repeat_stuff("Row ", 3) on line 3 is redundant and should be removed from your code. Otherwise, it looks good!

In case anyone else has this as well, my issue with this was that I put a space between repeat_stuff and the parenthesis: repeat_stuff ("Row ", 3). This prevented my code from clearing Step 2. When I deleted that space, it worked. But apparently having no space is not a requirement of python, because if I put the space back in it still clear step 3.

1 Like

Python’s PEP 8 style guide states that there should be no whitespace between the function name and the opening bracket.

Welcome to the forums!

1 Like

Thank you for clarifying! Will there be a version in the future where the compiler would be unable to read a space? I prefer it stylistically as it looks cleaner to me and is more familiar from how English is written.

It seems unlikely to me at least that they’d ever remove the option for spaces. I think it’s more likely though that any large project you may work on in the future may already have their own style requirements and maintaining consistency is important. It would be bad practice to try and make your code style different to that of your collaborators and may not be accepted. So whilst you could run code with spaces as you please but it might be a style you have to unlearn down the line, personally I’d recommend sticking with the convention unless there’s a very compelling reason not to.

2 Likes

ok got, (didnt see the answer here) it its case sensitive so "Row " (not "row ")

14 posts were split to a new topic: Help with string functions review

Hey, there, I’m stuck at part two, here’s my code:


As far as I can tell there is no space in front of repeat_stuff in Line 3, seperating, it from the function, and the space in "Row ", as suggested by the error message is in place. Does anyone know what prevents my code from passing the requirements?

I’m not sure of the exact test being performed but I’m wondering if there is a test for text matching a specific line. In this case you might want to bring your code into line with the standard style guidance-https://www.python.org/dev/peps/pep-0008/

In this specific case, I’d assume it’s the line repeat_stuff ("Row ", 3) that is causing issues. Remove the gap between the function name and the parentheses and it should then pass. Other alterations which may or may not be necessary include removing the spaces in the function definition def myfunc(mypar): would be the standard.

Removing the gap in Line 3 did, thanks for the advice. I wasn’t aware that there is a ‘correct’ way for spacing, but I suppose that’s part of the learning process

Python’s style guide recommends against the use of a space between the name of a function and the opening parentheses in a function call.

Your code would still run even with the space, but Codecademy is likely checking for exactly repeat_stuff("Row ", 3) rather than the output.

Currently stuck on Step 3:

Change the print statement inside repeat_stuff to a return statement instead.
It should return stuff*num_repeats .

not sure what they’re specifically trying to ask but i’ve tried everything I could think of to get past it but I’ve hit a wall.

Welcome to the forums!

Before Step 3, you should have used print statements to print out something like "Row Row Row ". Now, the exercise is asking you to return this instead.

While the print method prints out a message to the console, the return method is used to pass return value(s) back to the function call. This is useful if we want to obtain a value from a function and use it elsewhere in our code.

Comparison

print("bar") # prints bar

def foo():
  return "bar"

foo() # example of a call to the function, prints nothing

my_var = foo() # stores the returned value from the call to foo()
print(my_var) # prints bar

If I am not wrong in understanding your query, basically you expected
"Row Row Row Your Boat. " as output. But you have not used any print statement. If you use print(lyrics) , you will get the expected one.