But I only want to run the for loop as long as the sum is < 9000. So the for loop runs while sum <9000. I can make it work by taking out the “while” and using “if sum > 9000 // break” instead, but I want to understand why the “while” approach is wrong?
it doesn’t work like that. The for loop will make all its iterations (loop over all elements in list)
The instructions state:
The function should sum the elements of the list until the sum is greater than
sum has reached exactly
9000 and there are still additional elements to process, we’re not done yet. A common source of software bugs is failure to account for edge cases.
I’m looking for some feedback on my answer here. My code returns the right answer, but I ALSO get the error “list index out of range”. What do I need to improve in my code? Any feedback is appreciated! Thank you!
#Write your function here def over_nine_thousand(lst): count = lst indice = 1 while(count < 9000 and indice < len(lst)): count += lst[indice] indice += 1 else: return count #Uncomment the line below when your function is done print(over_nine_thousand([8000, 900, 120, 5000]))
The exercise might have a test-case/corner case where you get an empty list. Which would a problem for your code
hi, i wonder why the second one is not working.
why is it indentation error?
Hey i am wondering why these are not correct:
1: def over_nine_thousand(lst): sum=0 for n in lst: sum+=n if sum >= 9000 and len(lst)>0: break return sum print(over_nine_thousand([8000, 900, 120, 5000])) The output will be none instead of 9020. 2: def over_nine_thousand(lst): sum=0 for n in lst: sum+=n if sum >= 9000 and len(lst)>0: break return sum print(over_nine_thousand([8000, 900, 120, 5000])) This one would be 8000.
Can someone explain why these two scripts are wrong? From my perspective, they are the same as the correct solution.
they are absolutely not the same as the solution. Both don’t cover the edge case of an empty list. If the list is empty, the loop never runs, so the return keywords are never reached
in the first example,
break would break the loop, so again, the return keyword isn’t reached.
its important to understand that when a return keyword is reached, data is handed back to the caller which signals the function is done executing
so in your second code sample, the return keyword is reached in the first iteration of the loop, handing back the sum of the first value.
This was my code for the Over 9000 exercise:
def over_nine_thousand(lst): sum1 = 0 for num in lst: if sum1 <= 9000: sum1 += num elif sum1 > 9000: return sum1 elif len(lst) == 0: return 0 print(over_nine_thousand([8000, 900, 120, 5000]))
I get the correct output for the exercise, yet this message appears at the bottom:
“over_nine_thousand([8000, 900]) should have returned 8900, and it returned None”
Thus, I am not able to progress. I checked the code in my terminal and achieved the same output.
The troubleshooting I tried involved attempting to print the value of “sum1”. The value would not print, however, no matter where I put the print command and no matter the indentation I tried, so my troubleshooting hit a brick wall (as far as trying to get the output to reveal any clues for me goes).
Try running the code in your head for the input given in the error message. The SCT for the exercise tests input other than the list given in the
[8000, 900]? The message states that it should return 8900 which is correct, no? Your code returns
None for that input. If you follow what happens with your finger or in your head, you’ll see that
None is returned. If the code inside a function is finished executing without a
return statement being executed, the function implicitly returns
this was probably already suggested by someone else, but please bear with me.
I did the following as an answer, then tested it with the print and with and empty list, both were expected results as per the assignment, yet the exercise keep telling me “index out of range” - can you please help me understand ? - thanks
here’s my code:
#Write your function here def over_nine_thousand(lst): sumlst = 0 i = 0 if len(lst) > 0: while sumlst < 9000 and i <= len(lst): sumlst += lst[i] i += 1 return sumlst else: return 0
actually, if you have a list of which the sum is less then 9000:
print(over_nine_thousand([100, 200, 300]))
you get the error. That is the edge case you missed.
Thanks a lot - much clearer now.