15/7 Why is my code not working?


#1

def reverse(text):
output = ''
for x in range(0, len(text), -1):
output += text[x]
return output

Somehow it only returns an empty string instead of the reversed word. Am I missing something really obvious?


#2

you wrote this in your for loop

for x in range(0, len(text), -1):

writing it this way the numbers will begin from 0 to len(text) but you want the numbers to go from the length of the text downwards. the reason why its not printing is because of the -1 argument provided
change that to

for x in range(len(text)-1, -1, -1):

t


#3

It worked! Thanks for explaining it so clearly - now I understand where I went wrong!


#4

first off i know my code can be improved greatly this is just what i came up with first, but its giving me an error but when i type in print reverse("Python!) i get the correct answer on my output screen but codec says im getting it wrong
name = []

def reverse(text):
    rever = ""
    length = len(text)
    while length > 0:
        length -= 1
        name.append(text[length])
    for s in name:
       rever += s
    return rever

#5

This is my code and works:

def reverse(texto):
invertido = ""
cont = len(texto)
indice = -1
while cont>=1:
invertido +=texto[indice]
indice=indice+(-1)
cont -= 1
return invertido