Python:数字总和的错误输出

时间:2015-08-19 07:38:10

标签: python python-3.x

运行programme时输出错误。我使用python 3.4.3版本.Below是我的程序代码,

CODE: -

from pip._vendor.distlib.compat import raw_input

def sumOfDigits(n):
    summ=0;
    while(n!=0):
        r = n%10;
        summ+=r;
        n/=10;
    return summ;


input_num = raw_input("Enter a number : ");
n = int(input_num);

print("sum of digits of the number %s is %d" % (input_num,sumOfDigits(n)));

输出: -

Enter a number : 54928
sum of digits of the number 54928 is 31

1 个答案:

答案 0 :(得分:1)

尝试:

def sumOfDigits(n):
r = 0
while n:
    r, n = r + n % 10, n / 10
return r


input_num = raw_input("Enter a number : ");
n = int(input_num);

print("sum of digits of the number %s is %d" % (input_num,sumOfDigits(n)));

完美地为我工作。