問題是在不使用len(list)的情況下對列表中的元素進行計數。統計列表中的元素
我的代碼:
def countFruits(crops):
count = 0
for fruit in crops:
count = count + fruit
return count
錯誤是:「詮釋」和「海峽」
這些都應該是要運行的程序的測試案例。
crops = ['apple', 'apple', 'orange', 'strawberry', 'banana','strawberry', 'apple']
count = countFruits(crops)
print count
7
在第一時間通過你的for循環,你問的Python添加'0'和''apple''。 Python不知道如何做到這一點,所以會引發錯誤。你能做到這一點,所以你只是在一起添加數字嗎? –
'count = count + 1'或簡單地'count + = 1' – root
'count = crop .__ len __()'?. (除非你認爲這是作弊:D) – kampu