samedi 24 octobre 2020

How to reverse a string in python using if statements

This is my code so far, but I am trying to display olleH,dlroW instead of dlroW, olleH (Hello World). What is wrong with my code. Also i've seen examples that use for statements to reverse a string. But I would like to stick to if else statements (recursion).

def reverse_recursion(string):
    if len(string) == 0:
        return string
    else:
        return reverse_recursion(string[1:]) +s[0] 

Aucun commentaire:

Enregistrer un commentaire