python教程案例分析_Python 练习实例29

#14

叮咚

126***9648@qq.com

4

其他解法

#!/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***9648@qq.com4年前 (2017-04-07)

#13

叮咚

126***9648@qq.com

5

其他参考解法:

#!/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***9648@qq.com4年前 (2017-04-07)

#12

mythwind

774***013@qq.com

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

mythwind

774***013@qq.com4年前 (2017-04-11)

#11

未来星

mas***ing2010@163.com

1

其他参考解法:

#!/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***ing2010@163.com4年前 (2017-04-27)

#10

等一个人

252***465@qq.com

4

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***465@qq.com4年前 (2017-05-03)

#9

swordzjc

hfu***9@163.com

2

参考方法:

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

swordzjc

hfu***9@163.com4年前 (2017-05-04)

#8

菜鸟py

928***115@qq.com

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

菜鸟py

928***115@qq.com4年前 (2017-05-18)

#7

qingfeng

297***734@qq.com

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

qingfeng

297***734@qq.com4年前 (2017-06-01)

#6

朦胧

253***5732@qq.com

0

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***5732@qq.com4年前 (2017-06-12)

#5

fish

353***832@qq.com

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

fish

353***832@qq.com3年前 (2017-06-30)

#4

colinshi

col***shi@hotmail.com

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

colinshi

col***shi@hotmail.com3年前 (2017-07-06)

#3

CosmosHua

cos***cosmos@163.com

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

CosmosHua

cos***cosmos@163.com3年前 (2017-07-13)

#2

按剑当歌

yan***8908038@163.com

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***8908038@163.com3年前 (2017-08-02)

#1

苏格拉顶

qia***avie@163.com

0

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***avie@163.com3年前 (2017-11-20)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值