不多于 5 位的正整数逆序输出(python)

写了一个看了虎躯一震的代码
题目为:

【if 语句】从键盘上输入一个不多于 5 位的正整数,编写程序实现如下要求:

(1)求出它是几位数;
(2)分别输出每一位数字;
(3)按逆序输出每位数字,例如原数为 321,应输出 123。

下面贴上源代码(python)

import math

number=int(input("请输入一个不多于5位的正整数:"))
digit=0;
number_1=0
number_2=0
number_3=0
number_4=0
number_5=0
if number>=10000:
    digit=5
    number_5=math.floor(number/10000)
    number_4=math.floor(number/1000)-number_5*10
    number_3=math.floor(number/100)-number_5*100-number_4*10
    number_2=math.floor(number/10)-number_5*1000-number_4*100-number_3*10
    number_1=math.floor(number/1)-number_5*10000-number_4*1000-number_3*100-number_2*10
elif number>=1000:
    digit=4
    number_4=math.floor(number/1000)
    number_3=math.floor(number/100)-number_4*10
    number_2=math.floor(number/10)-number_4*100-number_3*10
    number_1=math.floor(number/1)-number_4*1000-number_3*100-number_2*10
elif number>=100:
    digit=3
    number_3=math.floor(number/100)
    number_2=math.floor(number/10)-number_3*10
    number_1=math.floor(number/1)-number_3*100-number_2*10
elif number>=10:
    digit=2
    number_2=math.floor(number/10)
    number_1=math.floor(number/1)-number_2*10
elif number>=0:
    digit=1
    number_1=math.floor(number/1)
    
    
print(str(number)+"是一个"+str(digit)+"位数")


if number_5!=0:
    print("万位数字为:"+str(number_5))
if number_4!=0:
    print("千位数字为:"+str(number_4))
if number_3!=0:
    print("百位数字为:"+str(number_3))
if number_2!=0:
    print("十位数字为:"+str(number_2))
if number_1!=0:
    print("个位数字为:"+str(number_1))
    

print("逆序输出为:")
if digit==1:
    print(str(number_1))
elif digit==2:
    print(str(number_1)+str(number_2))
elif digit==3:
    print(str(number_1)+str(number_2)+str(number_3))
elif digit==4:
    print(str(number_1)+str(number_2)+str(number_3)+str(number_4))
elif digit==5:
    print(str(number_1)+str(number_2)+str(number_3)+str(number_4)+str(number_5))

运行结果当然也相当的nice:
不多于 5 位的正整数逆序输出(python)_第1张图片

你可能感兴趣的:(python,python,开发语言,后端)