Board logo

標題: Python遞迴練習一 [打印本頁]

作者: 李泳霖    時間: 2023-5-17 15:31     標題: 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))
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2