the question asks to find the sum of odd numbers when given a string of numbers.
(该问题要求在给定一串数字时查找奇数之和。)
so for example, if we are given "123" we should get the sum of 4. (因此,例如,如果给定的值为“ 123”,则总和为4。)
This is my attempt and it returns '4' which is incorrect
(这是我的尝试,返回“ 4”,这是不正确的)
def sumoddnum(s):
total= 0
for i in range(len(s)):
if i % 2 == 1:
total += i
return total
print(sumoddnum('12345'))
I've also tried converting s into integers but it keeps giving me the "int is not iterable" error
(我也尝试过将s转换为整数,但它一直给我“ int不可迭代”错误)
def sumoddnum(s):
total= 0
s= int(s)
for i in s:
if i % 2 == 1:
total += i
return total
print(sumoddnum('12345'))
ask by jennyislong translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…