实用:python中用户输入任意数字,判断数字的位数和实现正反向打印

num=int(input("please input a number:"))
tail_num=num
count=0
while True:
    print(num%10)
    num=num//10
    count+=1
    if num==0:
        print('you input {} numbers'.format(count))
        break
print('---------',tail_num,count,'----------')
for i in range(count,0,-1):
    digit=10**(i-1)
    head_num=tail_num//digit
    tail_num-=head_num*digit
    print(head_num,tail_num)

输出如下:

please input a number:2131
1
3
1
2
you input 4 numbers
--------- 2131 4 ----------
2 131
1 31
3 1
1 0

 

你可能感兴趣的:(Python学习记录)