15/7 Why is my code not working?


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?


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



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


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
    for s in name:
       rever += s
    return rever


This is my code and works:

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