2.1 reverse


#1

Hi everyone,

I have some troubles with this. Here you find my code



def reverse(text):
  text = raw_imput('give me the string to reverse')
  for i in text:
    reversed = []
    reversed = i + reverse
  print reversed

and that is the error I got it:

Did you create a function called reverse? Your code threw a "global name 'raw_imput' is not defined" error.

what is wrong?
thank you everyone for the help you can give me

best
Salvo


#2

just a typo. raw_input not “raw_imput”


#3

ah sorry I corrected but it just changed the error:

Does your reverse function take exactly one argument (a string)? Your code threw a “coercing to Unicode: need string or buffer, function found” error.

That I actually do not understand because for what I read raw_input gets input from the user and returns the data input by the user in a string. right?


#4

ok, I think that theoretically it works:

my code is:

def reverse(text):
reversed = ""
for i in text:
reversed = i + reversed
print reversed

reverse(“salvo”)

than this what is happening in the right part of the screen:

than I have this error:

Your function fails on reverse(“Python!”). It returns “None” when it should return “!nohtyP”.

what is meaning? I mean, for me it seems is actually working


#5

you may need to repost your question in a different thread to get someone else’s attention. I dont know python.


#6

@asernot

haha, you made a silly mistake! You forgot to return reversed after you printed it.
Your code works well!


#7

@coursemaster50025

ahahah sorry, yes I forgot.

I have just a question why even if I forgot return it was “kind of working”? I am thinking without return it should not print reversed, right?


#8

No, it can print reversed. You just have to return reversed AFTER you print it, or else the function won’t work.


#9

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