作业帮 > 英语 > 作业

新手python问题Write a function called digit_sumthat takes a posi

来源:学生作业帮 编辑:作业帮 分类:英语作业 时间:2024/04/29 09:26:38
新手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.)
这个要怎么写啊,新手不会做了,来求教.
不知道你给的数据是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
再问: 是codecademy上面的题目,图片是题目描述。谢谢了。
再答: 应该是10进制的
再问: 提交以后对了,能给我讲解一下吗。新人不是很懂...
再答: 很简单啊,10进制就是余数相加而已