標題:
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
def squareOddsum(num):
if(len(num)==0):
return 0
elif num[0]%2==0:
return squareOddsum(num[1:])
else:
return (num[0]*num[0])+squareOddsum(num[1:])
a=input('請輸入列表:')
list123=list(map(int, a.split()))
print(squareOddsum(list123))
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2