Loop question about the Python 3 coding challenges

So I was working on a piece of code in one of the exercises and I built the below before I looked at the solution:
#Write your function here

def over_nine_thousand(lst):
sum_of_nums = 0
count = 0
if len(lst) == 0:
return sum_of_nums
elif sum(lst) < 9000:
return sum(lst)
else:
while sum_of_nums < 9000:
sum_of_nums += lst[count]
count += 1
return sum_of_nums

#Uncomment the line below when your function is done

print(over_nine_thousand([8000, 900, 120, 5000]))

The solution code was:
def over_nine_thousand(lst):
sum = 0
for number in lst:
sum += number
if (sum > 9000):
break
return sum

Mine worked but is much longer than the solution provided. My question is: Is there anything wrong with what I did? Why would I use the provided solution instead of mine/what is wrong with mine other than length and possibly readability?

Thanks!

The specific lesson in question can be found here: Learn Python 3 | Codecademy

There are multiple ways of doing anything in programming. The one you made and the one CC provides are only 2 of the basically infinite solutions to the problem. So now there is nothing wrong with how you do it. It’s just another way of solving the problem. Also, don’t forget to format your code: [How to] Format code in posts

1 Like

Awesome! Thanks for the input. Also, thanks for the format link. I didn’t know about that.

1 Like