定义一个函数,已知 a 是数列,列如 a = [2, 6, 8...95, 96, 98],求 a 中数值为奇数的总和
a = [2, 6, 8...95, 96, 98]
def odd_sum(li): tmp_list = [x for x in li if x % 2 != 0] return sum(tmp_list) a = [2, 6, 8, 9, 11, 12, 13, 95, 96, 98] print(f"奇数总和:{odd_sum(a)}")