15/7 reverse not passing


#1

So here's my code. I checked the output and it checks out fine. So why doesn't it check out? keep getting error:
Your function fails on reverse("Python!"). It returns "None" when it should return "!nohtyP".

def reverse(text):
    reverse_text = []
    count = 1
    t_len = len(text)
    for i in text:
        reverse_text.append(text[t_len-count])
        count+=1
    print reverse_text
reverse("Python!")

#2

The first thing i see is that you print the reverse text:

print reverse_text

you should use return instead of print. Now i need to check if your code for the rest is correct, give me a second.

for next time, use one of the two following options to make your code/indent is visible:

select your code and press ctrl + shift + c (or cmd + shift + c if you use a mac)

if this instructions are unclear, you can also insert 3 backticks before and after your code, like so:

```
<p>visible</p>
```

the backtick is located above the tab key on your keyboard


#3

For the rest, your code seems fine. Since you now use return (the function gives something back) you might want to call print on function call:

print reverse("Python!")

#4

alright switching to using return on the function works. Thanks a bunch. Also what did you mean make my indent visible by using ctrl + shift + c?


#5

You probably need to make the reverse_text as tuple, instead of list.
so first use reverse_text = '' to initiate
and in loop, use reverse_text+=(text[t_len-count])

this makes your result a string, instead of a list of characters.


#6

@segalou, good catch.

well, without ctrl + shift + c your code looks like this:

def reverse(text):
reverse_text = []
count = 1
t_len = len(text)
for i in text:
reverse_text.append(text[t_len-count])
count+=1
print reverse_text
reverse("Python!")

with ctrl + shift + c it looks like this:

def reverse(text):
    reverse_text = []
    count = 1
    t_len = len(text)
    for i in text:
        reverse_text.append(text[t_len-count])
        count+=1
    print reverse_text
reverse("Python!")

and with backticks:

def reverse(text):
    reverse_text = []
    count = 1
    t_len = len(text)
    for i in text:
        reverse_text.append(text[t_len-count])
        count+=1
    print reverse_text
reverse("Python!")

And indeed, you need the result in a string, not a lists


#7

3 posts were split to a new topic: 7/15 reverse


#8

Hi! I'm confused by this one bit of code. It seems that when you insert text in front of the [t_len - count] it appends the text and not the index of the items in text. How did you know to do this?