7. reverse. Hitting my head against the wall


#1



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

here's my idea for clarification.
I want to list every letter in x, in reverse, in y.
each time a letter passes through the while loop, z will count down by one. z is equal to the length of x
when z reaches zero, y will print.

I get the error message:
Your function fails on reverse("Python!"). It returns "['!']" when it should return "!nohtyP".

in previous versions of my code, it prints the last letter, then the rest of the word normally "!python". I don't know how to get it back to that state.

I could also use some help as to proper use of .join() in this context.


def reverse(x):
    x = str(x)
    y = []
    z = (len(x))
    while z != 0:
        for i in range(len(x)):
            y.append(x[i-1])
            z += -1        
            return y
    else:
        print y


#2

If you've only come as far as "!" then you shouldn't return yet

google for how to use join, plenty of examples, there's also docs.python.org but understanding what it says takes some careful reading

Should your function print or return the result? You're using both in your code.


#3

def reverse(x):
x = str(x)
y = []
z = (len(x))
while z != 0:
for i in range(len(x)):
y.append(x[i-1])
z += -1
else:
return '' .join(y)

reverse("Python!")

Alright, I'm getting somewhere!

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

It doesn't seem to be cycling through x as I want it to, could z be messing things up? I want it to count down the length of x until it hits zero and returns.

Also, I've noticed if I used print, it would print to the console but not the error message, and if I used return the opposite would happen. what is happening here?


#4

print out z to find out

print and return are different things, consider which you want.
(why would you be able to use them interchangeably? you shouldn't expect to be able to do that)


#5

Ugh this is taking too long. I just copied someone else's code so I can continue and come back to this later.

Z is working perfectly. It seems my loop is taking only the very last letter of the word, then prints the rest of it normally. How can I fix this?


#6

I would decide what it's supposed to do before writing anything. Can't describe the process of reversing before having figured out what that process is.

For example, does it make sense to use two nested loops for reversing? I would claim it doesn't, I would rip one of them out and continue from there to make it do exactly what I intend and nothing else.

And when you don't know what something's doing, then start thinking about how you can find out. Printing is a good first idea for doing so.


#7

Looked on Stackoverflow: http://stackoverflow.com/questions/38189760/reverse-exercise-python-codecademy-course-part-practice-makes-perfect-15-15

Turns out I needed to change the range.

def reverse(x):
    y = []
    z = (len(x))
    while z != 0:
        for i in range(len(x)-1, -1, -1):
            y.append(x[i])
            z += -1
    else:
        return '' .join(y)
reverse("Python!")

#8

def reverse(text):
reverse=[]
for char in range(len(text)-1,-1,-1):
reverse.append(text[char])
return "".join(reverse)
reverse("Python!")
works fine!


#9

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