Python 练习实例29
题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
程序分析:学会分解出每一位数。
程序源代码:
实例(Python2.x)
实例(Python3.x)
以上实例输出结果为:
请输入一个数: 23459 5 位数: 9 5 4 3 2
请输入一个数: 3472 4 位数: 2 7 4 3
题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
程序分析:学会分解出每一位数。
程序源代码:
以上实例输出结果为:
请输入一个数: 23459 5 位数: 9 5 4 3 2
请输入一个数: 3472 4 位数: 2 7 4 3
叮咚
126***[email protected]
其他解法
#!/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]
叮咚
126***[email protected]
其他参考解法:
#!/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]
mythwind
774***[email protected]
其他参考解法:
#!/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]
未来星
mas***[email protected]
其他参考解法:
#!/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]
等一个人
252***[email protected]
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]
swordzjc
hfu***[email protected]
参考方法:
#!/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]
菜鸟py
928***[email protected]
参考方法:
#!/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]
qingfeng
297***[email protected]
参考方法:
#!/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]
朦胧
253***[email protected]
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]
fish
353***[email protected]
参考方法:
#!/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]
colinshi
col***[email protected]
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-1colinshi
col***[email protected]
CosmosHua
cos***[email protected]
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]
按剑当歌
yan***[email protected]
参考方法:
#!/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]
苏格拉顶
qia***[email protected]
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]