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
提交
取消