7/15 Help please! What can I be doing different?


#1

Here’s my code:
{
def reverse(text):
last = (len(text) - 1)
while last >= 0:
print text[last],
last -= 1

}

And here is the output:

image

Where is the ‘cba’ coming from?

Thanks for any help!


#2

its very likely caused by the exercise validation, what if you include multiple function calls at the end of your script:

print reverse("hello world")
print reverse("reverse)

does it print both strings in reverse fine? A function should work multiple times


#3

It print those two like this:
image


#4

I also get an error message at the bottom of my editor that reads:

image


#5

It seems your function is not working correctly when having to deal with multiple function calls, why could that be?

Please copy paste your full code

None is the default returned value, it seems you only print the reversed string, you do not return it


#6

Thanks for the help, specifically about the lack of ‘return’.

Here’s the code that I ended with:

{def reverse(text):
backwards = []
last = (len(text) - 1)
while last >= 0:
backwards.append(text[last])
last -= 1
return “” .join(backwards)

print reverse(“Hello World!”)
print reverse(“Happy Birthday!”)}


#7

Seems good, any questions about it?


#8

None right now. Thanks again for your help!


#9

Aside:

For normal purposes, we generally do not precede a dot with a space.

 "".join()   vs.  "" .join()

It’s just a convention, but one that most people follow so good to be in the habit of.


#10

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