This function receives as input a string which may contain letters, digits or special symbols.The function should return the sum of adding the digits in the string. An example addDigitsInString('a12bcd3') should return 6
2 回答
翻过高山走不出你
TA贡献1875条经验 获得超3个赞
使用关键词 def 声明这是一个函数
1def 函数名 (参数):
2 语句块
参数可以没有,也可以有多个,用逗号隔开,第一行称为函数头,结尾一定要加冒号,代表开始进入函数体的执行。
语句块也就是函数体,是关于这个函数要实现的功能的语句,语句要有返回值即return语句,如果没有return语句,就代表return none.
慕工程0101907
TA贡献1887条经验 获得超5个赞
#! /usr/bin/env python# coding: UTF-8# python3.3 def func(test_str): '''deal with the str''' standard_list = [] for i in range(10): standard_list.append(str(i)) sum = 0 for str_ in test_str: if str_ in standard_list: sum = sum + int(str_) return sum if __name__ == '__main__': test_str = 'da1asda5ad3ad2' value = func(test_str) print(value)
添加回答
举报
0/150
提交
取消
