返回列表 發帖

Python遞迴練習一

Please write a recursive function squreOddsum(num) in Python that takes A Python list of integers and returns the sum of squares of all odd numbers in the list.if the list is empty ,the function should return 0.For example
  1. def squareOddsum(num):
  2.     if(len(num)==0):
  3.         return 0
  4.     elif num[0]%2==0:
  5.         return squareOddsum(num[1:])
  6.     else:
  7.         return (num[0]*num[0])+squareOddsum(num[1:])  
  8. a=input('請輸入列表:')
  9. list123=list(map(int, a.split()))
  10. print(squareOddsum(list123))
複製代碼
istak.teach2@gmail.com

返回列表