Python练习实例027

问题:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。

#! /usr/bin/env python3
# -*- coding:utf-8 -*-

# Author   : Ma Yi
# Blog     : http://www.cnblogs.com/mayi0312/
# Date     : 2020-06-22
# Name     : demo027
# Software : PyCharm
# Note     : 利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。


def fun(text: str):
    """
    将输入的字符倒序打印出来
    :param text: 字符串
    :return:
    """
    if len(text) == 1:
        print(text, end=" ")
    else:
        print(text[-1], end=" ")
        fun(text[: -1])


# 入口函数
if __name__ == '__main__':
    fun("Hello")

运行结果:

o l l e H 
原文地址:https://www.cnblogs.com/mayi0312/p/13176396.html