Latest web development tutorials

Python 練習實例27

Python 100例 Python 100例

題目:利用遞歸函數調用方式,將所輸入的5個字符,以相反順序打印出來。

程序分析:無。

程序源代碼:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

def output(s,l):
    if l==0:
       return
    print (s[l-1])
    output(s,l-1)
 
s = raw_input('Input a string:')
l = len(s)
output(s,l)

以上實例輸出結果為:

Input a string:abcde
e
d
c
b
a

Python 100例 Python 100例