新手python问题Write a function called digit_sumthat takes a positive integer n as input and returns the sum of all that number's digits.For example:digit_sum(1234)should return 10 which is 1 + 2 + 3 + 4.(Assume that the number you are given will
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/19 07:44:03
![新手python问题Write a function called digit_sumthat takes a positive integer n as input and returns the sum of all that number's digits.For example:digit_sum(1234)should return 10 which is 1 + 2 + 3 + 4.(Assume that the number you are given will](/uploads/image/z/12684322-10-2.jpg?t=%E6%96%B0%E6%89%8Bpython%E9%97%AE%E9%A2%98Write+a+function+called+digit_sumthat+takes+a+positive+integer+n+as+input+and+returns+the+sum+of+all+that+number%27s+digits.For+example%3Adigit_sum%281234%29should+return+10+which+is+1+%2B+2+%2B+3+%2B+4.%28Assume+that+the+number+you+are+given+will)
新手python问题Write a function called digit_sumthat takes a positive integer n as input and returns the sum of all that number's digits.For example:digit_sum(1234)should return 10 which is 1 + 2 + 3 + 4.(Assume that the number you are given will
新手python问题
Write a function called digit_sumthat takes a positive integer n as input and returns the sum of all that number's digits.
For example:digit_sum(1234)should return 10 which is 1 + 2 + 3 + 4.
(Assume that the number you are given will always be positive.)
这个要怎么写啊,新手不会做了,来求教.
新手python问题Write a function called digit_sumthat takes a positive integer n as input and returns the sum of all that number's digits.For example:digit_sum(1234)should return 10 which is 1 + 2 + 3 + 4.(Assume that the number you are given will
不知道你给的数据是10进还是16进的,两个都写一下好了
def digit_sum_16( number ):con = 0
while number:
con += number & 0xF
number >>= 4
return con
def digit_sum_10( number ):
con = 0
while number:
con += number % 10
number /= 10
return con