def add(string, option): even_list = [] odd_list = [] all_sum = 0 for num in string: num = int(num) all_sum += num if num % 2 == 0: even_list.append(num) if num % 2 != 0: odd_list.append(num) if option == 0: return all_sum if option == 1: return sum(odd_list) if option == 2: return sum(even_list)
- def add(string, option):
return 28 #total depending on the option- even_list = []
- odd_list = []
- all_sum = 0
- for num in string:
- num = int(num)
- all_sum += num
- if num % 2 == 0:
- even_list.append(num)
- if num % 2 != 0:
- odd_list.append(num)
- if option == 0:
- return all_sum
- if option == 1:
- return sum(odd_list)
- if option == 2:
- return sum(even_list)
test.assert_equals(add("1234567" , 0),28) test.assert_equals(add("1234567" , 1),16) test.assert_equals(add("1234567" , 2),12)
test.assert_equals(add("1234567" , 0),28)- test.assert_equals(add("1234567" , 0),28)
- test.assert_equals(add("1234567" , 1),16)
- test.assert_equals(add("1234567" , 2),12)