I’m trying to use a while loop to sum the elements of a list until the sum is over 9000. My while loop isn’t working as it seems to completely ignore the inequality. I’ve tried changing the indentation of the return but that doesn’t make a difference.
#Wrong code using a while loop def over_nine_thousand(lst): nine_thousand = 0 while nine_thousand <= 9000: for i in lst: nine_thousand += i return (nine_thousand)
I managed to make a working version that doesn’t use a while loop:
#Working code def over_nine_thousand(lst): nine_thousand = 0 for i in lst: nine_thousand += i if nine_thousand > 9000: break return (nine_thousand)
Why is my while loop not working? Is it possible to write a version with a while loop?