菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 基础教程
(追記) (追記ここまで)

Python 练习实例27

Python 100例 Python 100例

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

程序分析:无。

程序源代码:

实例(Python 2.0+)

#!/usr/bin/python# -*- coding: UTF-8 -*-defoutput(s,l): ifl==0: returnprint(s[l-1])output(s,l-1)s = raw_input('Input a string:')l = len(s)output(s,l)

实例(Python 3.0+)

#!/usr/bin/python3defoutput(s,l): ifl==0: returnprint(s[l-1])output(s,l-1)s = input('Input a string:')l = len(s)output(s,l)

以上实例输出结果为:

Input a string:abcde
e
d
c
b
a

Python 100例 Python 100例

AI 思考中...

9 篇笔记 写笔记

  1. #0

    微笑依旧

    dzh***[email protected]

    16

    使用负数下标:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    def desc_output(s):
     if(len(s) > 0):
     print(s[-1]) # python 负数下标
     desc_output(s[0:-1]) 
    s = raw_input('Input a string:')
    desc_output(s)
    

    微笑依旧

    dzh***[email protected]

    9年前 (2017年04月13日)
  2. #0

    等一个人

    252***[email protected]

    36

    Python3 下非递归,使用各列表的 reverse() 方法:

    #!/usr/bin/env python3
    S = input('Input a string:')
    L = list(S)
    L.reverse()
    for i in range(len(L)):
     print(L[i])

    等一个人

    252***[email protected]

    9年前 (2017年05月03日)
  3. #0

    aright

    157***[email protected]

    3

    参考方法:

    # coding:utf-8
    def output(s):
     s = list(s)
     if len(s) == 0:
     return
     print(s[len(s) - 1])
     s.pop()
     output(s)
    output("abcde")
    

    aright

    157***[email protected]

    9年前 (2017年06月13日)
  4. #0

    sssss

    lij***[email protected]

    0

    Python3 下测试:

    #!/usr/bin/env python3
    s=input("输入一串字符: ")
    s=list(s);
    for i in range(0,int(len(s)/2)):
     t=s[len(s)-i-1];
     s[len(s) - i - 1]=s[i]
     s[i]=t
    print("".join(s))

    sssss

    lij***[email protected]

    9年前 (2017年07月24日)
  5. #0

    程序渊

    846***[email protected]

    0

    以下实例,可以支持中文字符:

    # encoding:utf-8
    '''
    Created on 2017年8月14日
    题目:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。
    @author: wangtaoyuan
    '''
    import sys
    reload(sys)
    sys.setdefaultencoding("utf8")
    str = raw_input('请输入:').decode('utf-8')
    def reoutput(str):
     if len(str) == 0:
     return
     print str[-1]
     str = str[0: len(str) - 1]
     reoutput(str)
    reoutput(str)

    程序渊

    846***[email protected]

    9年前 (2017年08月14日)
  6. #0

    patty

    pat***[email protected]

    0

    参考方法:

    # -*- coding: UTF-8 -*-
    list = []
    for i in range(1,6):
     a = input('输入第 {} 个数字:'.format(i))
     list.append(a)
    list2 = list[::-1]
    print(list2)

    patty

    pat***[email protected]

    9年前 (2017年09月15日)
  7. #0

    大大大大大大大熊

    382***[email protected]

    20

    Python3 测试实例:

    s=input("请输入字符:")
    for i in range(len(s)-1,-1,-1):
     print(s[i],end="")
    

    大大大大大大大熊

    382***[email protected]

    9年前 (2017年09月19日)
  8. #0

    at159

    844***[email protected]

    1

    Python3 测试:

    def printlast(strinfo, index):
     if (index + 1) == len(strinfo):
     print(end="")
     else:
     printlast(strinfo, index + 1)
     print(strinfo[index], end="")
    printlast("abcdefg", 0)
    

    at159

    844***[email protected]

    9年前 (2017年10月11日)
  9. #0

    滑稽树上滑稽果

    374***[email protected]

    0

    Python3 参考:

    a=[]
    for i in range(1,6):
     b=input('请输入第'+str(i)+'个字符:')
     a.append(b)
    def fp(c):
     if c==6:
     return False
     print(a[5-c],end=' ')
     fp(c+1)
    fp(1)

    滑稽树上滑稽果

    374***[email protected]

    9年前 (2017年11月10日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /