# Over 9000

This was my original code:

``````def over_nine_thousand(lst):
sum = 0
if len(lst) == 0:
return sum
else:
for i in lst:
sum += i
if sum > 9000:
return sum
elif lst.index(i) == -1:
print(lst.index(i))
return sum

print(over_nine_thousand([8000, 900]))
``````

and it was outputting “None”

However changing the return statements to breaks instead fixed it:

``````def over_nine_thousand(lst):
sum = 0
if len(lst) == 0:
return sum
else:
for i in lst:
sum += i
if sum > 9000:
break
elif lst.index(i) == -1:
print(lst.index(i))
break
return sum

print(over_nine_thousand([8000, 900]))
``````

I’m confused mainly because of 2 reasons: why was my original codes result “None”? and I thought that return would both and the loop and the function when it was reached, is that assumption wrong?
Thanks!

Hi,
Your original code is fine, the returns should work fine. The problem is it fails to deal with one condition, which happens to be the one you’ve tried in your example.

If sum is still 9000 or less by the time the for-loop ends, the function will end with no return value (hence your ‘none’).
So, either way you need the return at the end of the function.

I have issue with the same problem.

def over_nine_thousand(lst): list1 = [] sum1 = sum(lst[:]) if sum1 == 0: return 0 elif sum1 <= 9000: return sum1 else: for x in lst: sum2 = sum(lst[0:lst.index(x)]) while sum2 <= 9000: list1.append(x) list1.append(lst[lst.index(x)+1]) return sum(list1[:])

I wrote this code but it doesn’t work. Could someone please point out what the issue is.