Stuck at reverse


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

<What do you expect to happen instead?>

```python

def reverse(string):
s = list(string)
for place in s:
mylist=[]
mylist.append(“s[0]”)

I DON´T KNOW HOW TO APPEND THE S[0] INTO THE LAST PLACE OF MYLIST

INSTRUCTIONS:

Define a function called reverse that takes a string textand returns that string in reverse. For example: reverse(“abcd”) should return “dcba”.

You may not use reversed or [::-1] to help you with this.

You may get a string containing special characters (for example, !, @, or #).

<do not remove the three backticks above>

#2

Declare the empty list before the loop, not inside it. We need to build the list inside the loop.

Appending will only reproduce the original list (if we append place). s[0] will always be the same thing.

Python also has an insert function that prepends a list.

for place in s:
    mylist.insert(place)

#3

the page says that this is the answer:

def reverse (text):
word=""
l=len(text)-1
while l>=0:
word = word + text[l]
return word

I DON´T GET THE WHILE LOOP, WHAT D0ES IT word= word +text[l] MEANS?


#4

UUUUUUH JUST GOT IT :slight_smile:

but could´ve done the code using a for loop?


#5

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