我需要幫助來獲得這個python函數。 一個遞歸函數count_upper_lower(),它接受一個非空字符串作爲其參數,並返回一個元組,其中包含字符串中有多少個字母大寫以及多少個小寫(按該順序)的計數。 例如使用遞歸計算Python中的大寫和小寫字母
print (count_upper_lower(’Town Hall University’))
將返回
(3, 15)
這是我迄今爲止
def count_upper_lower(word):
upper = 0
lower = 0
if word == "":
upper = 0
lower = 0
return upper, lower
elif word[0].isupper():
upper = 1 + count_upper_lower(word[1:])
return upper , lower
elif word[0].islower():
lower = 1 + count_upper_lower(word[1:])
return upper , lower
else:
upper = 0 + count_upper_lower(word[1:])
lower = 0 + count_upper_lower(word[1:])
return upper, lower
我收到以下錯誤:
TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
提示:通過「word ==」a「'調用。你期望'1 +(0,0)'是什麼? – timgeb