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


#1

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

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

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


#2

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

def reverse(text):
text1=""
for i in range(len(text)):
text1=text1+text[len(text)-i-1]
print text1

reverse('Python!')


#3

@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:


#4

hope this helps

def reverse(text):
    l=len(text)
    back=[]
    for t in range(0,len(text)):
        back.append(text[l-1])
        l-=1
    return "".join(back)

#5

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

MY CODE:

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

EDIT
WITH ENUMERATE()

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

#6

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


#7

Alternative simple way)

def reverse(text):
l=len(text)
a=''
while l:
l-=1
a+=text[l]
return a
print reverse('text!')


#8

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

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

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


#10

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?


#11

@cdnchris

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)

#13

This worked for me:

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


#14

This worked for me:

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

#16

My solution

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


#17

try this:

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

#18

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

#19

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


#20

@felicity_johnson_mai

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:
    print(True)

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!