新手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/11/09 09:20:45
新手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