如果我只是sum_digits
在这里阅读我的函数,这在我的脑海中是有道理的,但它似乎产生了错误的结果。任何提示?
def is_a_digit(s):
''' (str) -> bool
Precondition: len(s) == 1
Return True iff s is a string containing a single digit character (between
'0' and '9' inclusive).
>>> is_a_digit('7')
True
>>> is_a_digit('b')
False
'''
return '0' <= s and s <= '9'
def sum_digits(digit):
b = 0
for a in digit:
if is_a_digit(a) == True:
b = int(a)
b += 1
return b
对于函数sum_digits
,如果我输入sum_digits('hihello153john')
,它应该产生9