python - Sum the digits of a strings using type(int) -
python - Sum the digits of a strings using type(int) -
i have seen question when googled tried else run programme fails, help me figure out why method wrong.
def sumdigits(s): """assumes s string. if s 'a2b3dc' homecoming 5""" a=0 in string: if type(i) == int: a=a+1 homecoming sumdigits('ab23sdf');
this method homecoming me zero. why?
use isdigit instead.
i.isdigit()
when iterate string string, need check if given chararacter digit.
secondly string variable undefined, should utilize s instead.
python
Comments
Post a Comment