7. Reverse


#1

Exercise:
https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/1?curriculum_id=4f89dab3d788890003000096#

Error Message:
Oops, try again. Your function fails on reverse("Python!"). It returns "None" when it should return "!nohtyP".

So I originally had the below code. It seems to do everything except print without the whitespaces. After looking at a couple of existing questions, this code prints instead of returns so I get why it doesn't work. Quick question though, how do I return this without white space?

def reverse(text):
    length = len(text)
    for word in text:
        length -= 1
        print text[length],
        
print reverse("Python!")

I then went and rewrote it to the below code, and got the same error message:

def reverse(text):
    length = len(text)
    reverse_list = []
    for word in text:
        length -= 1
        reverse_list.append(text[length])
    return str(reverse_list)
        
print reverse("Python!")

#2

first method...
In last line when you print it,even though you're using comma after print ,It will create a space there!
hint**
Concatenate each character in new string(define an empty string) then return it

Second method..
Your second code works fine ,except last line..
reason..

x =[1,2,3]
 
str(x) # this is  '[1,2,3]' now not "123"

hint**
You can usejoin() method on your final list to convert it back to string..not str()method.


#3

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