【发布时间】:2016-09-09 20:22:43
【问题描述】:
编写一个接受字符串作为输入的 Python 函数。 该函数必须返回字符串中出现的数字 0-9 的总和,忽略所有其他字符。如果字符串中没有数字,则返回 0。
我的代码:
user_string = raw_input("enter the string: ")
new_user_string = list(user_string)
addition_list = []
for s in new_user_string:
if ( not s.isdigit()):
combine_string = "".join(new_user_string)
print ( combine_string)
else:
if ( s.isdigit()):
addition_list.append(s)
test = "".join(addition_list)
output = sum(map(int,test))
print ( output )
输出应该是:
Enter a string: aa11b33
8
我的输出:
enter the string: aa11b33
aa11b33
aa11b33
1
2
aa11b33
5
8
【问题讨论】:
标签: python python-2.7 python-3.x ipython