ここで自分の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