我打算寫一個函數,我可以用它來總結所有數字的有兩位數的列表。例如:我怎樣才能更好地寫這些功能纔有意義
a=[1,2,11,100,10]
should return 21 since 11 and 10 of the list are having two digits
我也想數的範圍爲(0,1,2,... 100000)
def solution(A):
for integer in A:
if integer > 9 and integer <100:
two_digits_array.append(integer)
return sum(two_digits_array)
我想這是正常的,所以在測試:
Example test: numbers= [47, 1900, 1, 90, 45]
print (solution(numbers))
wow it works perfectly and returns 182
但我想這些:
q= [1, 1000, 80, -91]
WRONG ANSWER (got 80 expected -11)
我怎麼能去關於它,爲什麼失敗。
貌似從-10到-99號也被認爲是有兩個數字。因此,在你的for循環中,你必須檢查:「如果整數> 9,整數<100或整數<-9和整數> -100」 – mkabanen