Python 練習實例27

Python 100例 Python 100例

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

程式分析:無。

程式源代碼:

實例(Python 2.0+)

#!/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例