7/15 Reverse. Why doesn't the program accept my code


Hi! I'm new here and English isn't my first language...

This is what I've tried:
def reverse(text):
tes = []
a = 0
for i in text:
for y in tes:
a = a + 1
b = len(text)
b =b -a
print tes[b],
t = "Python!"

When i try it with reverse(t), the program does work, but for some reason i can't advance to the next level...


I have the same problem with this code. It works but I can't advance

def reverse(text):
for i in range(len(text)):
print text1



@jcp0_4 @netblaster17652

After making the new reversed string, return it. Printing to the console is not the same as returning value to system. Hope this helps :smiley:


hope this helps

def reverse(text):
    for t in range(0,len(text)):
    return "".join(back)


I have another example to add for you to look at using list comprehension.


def reverse(word):
    return "".join([word[(-1) * (letters + 1)] for letters in range(len(word))])


def reverse(word):
    return "".join([word[(-1) * (number+ 1)] for number, letter in enumerate(word)])


I think you just need to return text1 instead of printing it


Alternative simple way)

def reverse(text):
while l:
return a
print reverse('text!')


Another sample code:

def reverse(text):
    length = len(text)
    rtext = ""
    while length > 0:
        for i in range(0,length):
            rtext += text[length-1]
            length -= 1
    return rtext

def reverse(text):
    lst = []
    for i in range(len(text)-1,-1,-1):
    return "".join(lst)

Worked fine for me.You might want to take a look.


Could you unpack this one a little - I'm trying to sort out how it works. in particular,

word[(-1) * (letters + 1)]

is baking my noodle. A negative index number starts from the end of an array?



You are correct a negative starts from the end of the data.

def reverse(word):
    return "".join([word[(-1) * (letters + 1)] for letters in range(len(word))])

What we see here is list comprehension, the very first part is what will go into the list when it is run. After that are the conditions that control the list comprehension, unlike a lambda function it can only use if or for as conditions. While there are a lot of things we can do with just these two, sometimes you have to use a lambda or a standard for loop.

Ok, to break it down is easy. Just convert it back to a standard for loop. Also let's use enumerate() too in this function.

def reverse(word):
    """You have remember that strings in python are immutable,
    so we have to ensure that we convert the string to a list first."""
    new_word = list()
    for number, letter in enumerate(word):
         new_word.append(word[(-1) * {number + 1)])
    return ' '.join(new_word)


This worked for me:

def reverse(text):
lst = ""
for i in range(len(text)-1, -1, -1):
lst += text[i]
return lst


This worked for me:

 def reverse(text):
    lst = ""
    for i in range(len(text)-1, -1, -1):
        lst += text[i]
    return lst


My solution

def reverse(text):
     txet = ""
     for l in text:
          txet = l + txet
      return txet


try this:

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


This is mine. I think that is simple.

def reverse(text):
    revtext = ''
    x = len(text) - 1
    while x >= 0:
        revtext += text[x]
        x -= 1
    return revtext


why does x have to be >=0 instead of just >0?



You don't even need that, scroll up to the code I posted and you will see. In python if a item is empty if you compare it to False they have equal value. That is because it is a 0 to python as is False. Anything with a value thus will return as True so all you have to do is

value = 'This is True'
if value:

Just ask the interpreter if it is True no need to check length because anything with a value thus has a length of a positive nature over 0!