Reverse


#1

def reverse(text):
    
    """First, I create a list which elements are the characters of the word in the reverse order
    """
    
    reverse_list_text=[]
    last_index = len(text)-1
    while last_index <= 0 :
        reverse_list_text.append(text[last_index])
        last_index-=1
        
    """ Finally, I add these elements for return the word in the reverse order 
    """
    
    reverse_word=''
    for element in reverse_list_text:
        reverse_word += element 
    return reverse_word

I don't undertand wath's wrong


#2

a loop will run for as long as the condition is true

your condition is false from the beginning, so the while loop never runs


#3

try to place "while last_index <= 0 :" with "while last_index >= 0 :"

or you can make it shorter and easier :

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

#4

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