My code looks like it doesn't work (and it doesn't) but codecademy says that it passes


#1



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


It works, but it shouldn't


def reverse(text):
    asd=""
    for i in text:
        i=i+asd
        asd=i
    return asd


#2

Why shouldn't it work?

That code is badly written, but it works as expected.


#3

For example, when I do reverse("Wowzers, gee bobby") it just says None

BTW as you say it is badly written, can you give me some tips on how to make it more efficient/less badly written?
I was just trying to make it in a unique way so I wouldn't be copying what everyone else was doing


#4

Try to print out the result of function call:

print reverse("Wowzers, gee bobby")

:slight_smile:


I guess that you already know answer:

def reverse(text):
    return text[::-1]

It does not make sense to play with loops when you can use reverse slice.

But that was one of the requirements of this exercise, so please do not take this as an offence. Codecademy has chosen this way of reinforcing knowledge and that's fine. Your code is a perfect solution for this exercise.


#5

ehh I am a complete newbie at python so I don't know all the shortcuts lol (i.e. Idk what a reverse slice even is)

I think the program is teaching us to do it the hard way on purpose though


#6

I am not a big fan of codecademy :slight_smile: You are right, of course :slight_smile: I understand that they want you to reinforce your knowledge about loops. But it's not like we don't use a for loop in professional projects. There are situations where for loop is the best solution and where it simply does not make any sense to use it.

I am simply afraid that some users even after learning about [::-1] might still use overly long solutions to solve such a simple problem. The main advantage of Python is speed of writing the code. Do you see my point? :wink:


#7

I think that you're missing the point of doing it the way Codeacademy 'wants' you to do it.

As someone who just started Python as well, your way makes me sense in day-to-day kind of thing. But it's near impossible to grasp as a beginner.

Also, could you explain what "[::-1]" exactly does?


#8

I think that you're missing the point of doing it the way Codeacademy 'wants' you to do it.

So this is not to reinforce the knowledge about the currently taught syntax? Now you have my curiosity. Please, enlighten me.


As someone who just started Python as well, your way makes me sense in day-to-day kind of thing. But it's near impossible to grasp as a beginner.

That's not true. Slicing is covered in one of the next sections. This is not more complicated concept than for loop or list comprehension. This is basically one of the most basic methods. It's just order of sections in the codecademy course that implies that slicing is more complicated than loops.


Also, could you explain what "[::-1]" exactly does?

Sure, it reverses the list. It basically says: get elements from the start of the list to the end of the list starting from the end of the list.


#9

oh, in the problem it says we are not allowed to do [::-1] or reverse[...]


#10

I know, that is why I said:

It does not make sense to play with loops when you can use reverse slice.

But that was one of the requirements of this exercise, so please do not take this as an offence. Codecademy has chosen this way of reinforcing knowledge and that's fine. Your code is a perfect solution for this exercise.

:slight_smile:


#11

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