python:从键盘上输入一个三位整数(首先要确保输入的数是三位数),计算输出各位数之和。

num = eval(input("请输入一个三位数:"))
if 99 < num <= 999:
    hundred = num // 100
    decade = num // 10 % 10
    the_unit = num % 10
    n_sum = hundred + decade + the_unit
else:
        num = eval(input("你输入的不是三位数,请重新输入:"))
        if 99 < num <= 999:
            hundred = num // 100
            decade = num // 10 % 10
            the_unit = num % 10
            n_sum = hundred + decade + the_unit
print("百位数为{},十位数为{},个位数为{},各位数之和为{}".format(hundred,decade,the_unit,n_sum))

执行结果为:
python:从键盘上输入一个三位整数(首先要确保输入的数是三位数),计算输出各位数之和。_第1张图片

你可能感兴趣的:(python,python)