对一个范围的两个数进行数位的累加,例如有两个数 15
,19
则 他们的数位和应该为:
1+5+1+6+1+7+1+8+1+9,结果为40。
测试说明
样例1
输入:15
19
输出:
40
以下是不同方法实现方式:
代码1
class Task: def getSum(self,num1,num2): sum_num = 0 input_num = [num1, num2 + 1] a = list(range(*input_num)) for i in a: j = list(map(int, str(i))) c = sum([item for item in j]) sum_num = sum_num + c return sum_num
代码2
num1 =int(input('请输入数字:')) num2 = int(input('请输入数字:')) sum_num = 0 input_num =[num1,num2+1 ] print(list(range(*input_num))) a=list(range(*input_num)) print(a) for i in a: j=list(map(int, str(i))) c = sum([item for item in j]) sum_num=sum_num+c print(sum_num)
代码3
class Task: def getSum(self,num1,num2): sum = 0 for i in range(num1,num2+1): # k = i # print(k) while (i!=0): sum = sum +i%10 # print(sum) i = i//10 # print("+{:.0f}".format(k)) print(sum) return sum ########## END ########## t = Task() t.getSum(1000,1002)