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

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

Python 练习实例29

Python 100例 Python 100例

题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。

程序分析:学会分解出每一位数。

程序源代码:

实例(Python2.x)

#!/usr/bin/python# -*- coding: UTF-8 -*-x = int(raw_input("请输入一个数:\n"))a = x / 10000b = x % 10000 / 1000c = x % 1000 / 100d = x % 100 / 10e = x % 10ifa != 0: print"5 位数:",e,d,c,b,aelifb != 0: print"4 位数:",e,d,c,b, elifc != 0: print"3 位数:",e,d,celifd != 0: print"2 位数:",e,delse: print"1 位数:",e

实例(Python3.x)

#!/usr/bin/pythonx = int(input("请输入一个数:\n"))a = x // 10000b = x % 10000 // 1000c = x % 1000 // 100d = x % 100 // 10e = x % 10ifa != 0: print("5 位数:",e,d,c,b,a)elifb != 0: print("4 位数:",e,d,c,b)elifc != 0: print("3 位数:",e,d,c)elifd != 0: print("2 位数:",e,d)else: print("1 位数:",e)

以上实例输出结果为:

请输入一个数:
23459
5 位数: 9 5 4 3 2
请输入一个数:
3472
4 位数: 2 7 4 3

Python 100例 Python 100例

AI 思考中...

14 篇笔记 写笔记

  1. #0

    叮咚

    126***[email protected]

    12

    其他解法

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    print( '请输入大于10的数字:' )
    n=input()
    x=[]
    i=0;
    while(n!=0):
     x.append(n%10)
     i+=1
     n/=10
    print( '该数有 %d 位\n' %i )
    print( '逆序为:\n')
    print( x[::] )
    

    输出实例:

    请输入大于10的数字:
    12345
    该数有 5 位
    逆序为:
    [5, 4, 3, 2, 1]
    

    叮咚

    126***[email protected]

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

    叮咚

    126***[email protected]

    16

    其他参考解法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    print '输入大于10的数字:'
    n=input()
    x=str(n)
    for i in range(len(x)-1,-1,-1):
     print x[i], # , 号设置不换行
    

    测试输出结果:

    输入大于10的数字:
    12345
    5 4 3 2 1
    

    叮咚

    126***[email protected]

    9年前 (2017年04月07日)
  3. #0

    mythwind

    774***[email protected]

    1

    其他参考解法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    print( '请输入大于10的数字:' )
    a =input()
    if (len(str(a)) > 0) and (len(str(a)) <= 5) :
     print "%s 是 %d 位数" %(a, len(str(a)))
     newstr = str(a)[::-1]
     print newstr
     for i in newstr:
     print i
    

    mythwind

    774***[email protected]

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

    未来星

    mas***[email protected]

    5

    其他参考解法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    def output(num,l):
     if l == 0:
     return
     print (num[l-1]),
     output(num,l-1)
    num = raw_input('输入小于5位正整数 :' )
    l = len(num)
    output(num,l)
    print '\n长度为: %d' % l
    

    未来星

    mas***[email protected]

    9年前 (2017年04月27日)
  5. #0

    等一个人

    252***[email protected]

    14

    Python3 下使用列表的 reverse 方法:

    #!/usr/bin/env python3
    num = list(input('输入一个最多5位的数字:'))
    print(len(num))
    num.reverse()
    for i in range(len(num)):
     print(num[i], end='')
    

    等一个人

    252***[email protected]

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

    swordzjc

    hfu***[email protected]

    3

    参考方法:

    #!/usr/bin/python3
    # coding:utf-8
    s = str(input())
    def fun(m):
     if len(m) == 1:
     return m[0]
     else:
     return (m[len(m) - 1] + fun(m[:(len(m) - 1)]))
    if len(s) > 5:
     print("输入数字超过限定位数,输入无效")
    else:
     print('数位:%s\n输入的数字:%s\n逆序数字:%s' % (len(s), s, fun(s)))
    

    swordzjc

    hfu***[email protected]

    9年前 (2017年05月04日)
  7. #0

    菜鸟py

    928***[email protected]

    1

    参考方法:

    #!/usr/bin/python
    # coding:utf-8
    arr=[]
    def out_num(n):
     length = len(str(n))
     print "%d位数"%length
     for i in range(1, length+1):
     arr.append((n % (10**i))/10**(i-1))
     print arr
    out_num(245984)
    

    菜鸟py

    928***[email protected]

    9年前 (2017年05月18日)
  8. #0

    qingfeng

    297***[email protected]

    1

    参考方法:

    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-
    import sys
    s = 12345
    s = str(s)[::-1]
    print '%d 位数' % len(s)
    for i in range(len(s)):
     sys.stdout.write(s[i]+' ')
    

    qingfeng

    297***[email protected]

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

    朦胧

    253***[email protected]

    2

    Python3 测试实例:

    # coding:utf-8
    num=int(input("请输入一个正整数:"))
    def fn(s):
     if len(s)==1:
     return(s[0])
     else:
     a=s[-1]
     s=s[:-1]
     return(a+fn(s))
    while 1:
     if num<=0 or len(str(num))>5:
     num=int(input("输入错误,请重新输入:"))
     else:
     num=str(num)
     print()
     print("它是%d位数" % len(num))
     print("逆序打印:",fn(num))
     break
    

    朦胧

    253***[email protected]

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

    fish

    353***[email protected]

    0

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    def num(strl):
     l = len(str(strl))
     lis = []
     for i in range(l):
     x = strl % 10
     strl /= 10
     lis.append(x)
     print "长度为:{0}, \n逆序为:{1}".format(l,''.join(str(x) for x in lis) )
    num(123)
    

    fish

    353***[email protected]

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

    colinshi

    col***[email protected]

    1

    Python3 下测试:

    #!/usr/bin/python3
    x=input('请输入一个数:\n')
    a = len(x)
    print('这是一个{}位数'.format(a))
    b = -1
    while a != 0:
     a -= 1
     if b == -1:
     print(x[-1:],end=' ')
     b=b-1
     else :
     print(x[b:b+1],end=' ')
     b=b-1

    colinshi

    col***[email protected]

    9年前 (2017年07月06日)
  12. #0

    CosmosHua

    cos***[email protected]

    1

    Python3 参考实例:

    #!/usr/bin/python3
    def backn(n):
     ls = str(n); s = len(ls)
     ls = ls[s-1:0:-1] + ls[0]
     return ls, s
    #测试:
    backn(123456) #输出:(['654321'], 6)

    CosmosHua

    cos***[email protected]

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

    按剑当歌

    yan***[email protected]

    1

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
     
    num1=int(input("Please input a int <10000:"))
    for i in range(4,1,-1):
     if num1>10**i:
     print(i+1)
     break
    list1=[]
    for j in range(i,-1,-1):
     list1.append(int(num1/(10**j)))
     num1=num1%(10**j)
    for k in range(i,-1,-1):
     print(list1[k])

    按剑当歌

    yan***[email protected]

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

    苏格拉顶

    qia***[email protected]

    1

    Python3 参考:

    number = 12344
    print("数字%d," % number, end="")
    n = 0
    result = ""
    while number > 0:
     result += str(number % 10)
     number //= 10
     n += 1
    print("是%d位数, 逆序: %d" % (n, int(result)))

    苏格拉顶

    qia***[email protected]

    9年前 (2017年11月20日)

点我分享笔记

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

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