FAQ: Code Challenge: Loops - Over 9000


#1

This community-built FAQ covers the “Over 9000” exercise from the lesson “Code Challenge: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Over 9000

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

I’ve noticed the code works the best if your sum variable is named power_level


#3

Hello! I was wondering why we use a “for” loop in this and not a “while” loop. To me it seems more straightforward to say “while number < 9000…” but maybe there is something I am missing? I also got the correct answer with a while loop but it didn’t accept my code as a “solution” so I feel like maybe there is a consequence to using the while loop that I hadn’t thought of. Thanks!


#4

Hi, I used a while loop and had my code accepted just fine. But I did notice that it didn’t accept my code until the functions would return a list with a sum less than 9000 and return 0 if the list was empty to start with.


#6

Hello Everyone,

Its been three months of learning Python, spending hours at a time on the Codecademy’s site and I seem to be still lost as to how to best answer these coding challenges without having to click the solution button.

Moreover, is it my imagination or does it seem as though each challenge question may have parts of things that were previously taught, but the series of steps to solve each new problem vary to the point where every new method requires a different set of steps to solve. And I still don’t feel confident to answer any Python related questions in a forum setting.

It feels like I’m supposed to connect the dots but the dots aren’t on the page. It’s a little discouraging because I’m starting to feel incompetent. I know what for loops are, what is a while loop, and I understand most of the syntax when it is explained in the hints section or after I see it in the solution, but perhaps I’m missing some sort of creative element in figuring out how to solve these challenge questions.

I would like to know how others are able to solve these challenge questions or is everyone else racking their brain and revisiting past assignments in the hopes that something will jump out and make sense.

As a side note: Can anyone tell me if once we complete the codecademy course, are we in a position to begin seeking employment, or is this only a hobby level of skills.

Any response welcomed and thanks.


#7

Can you solve the problems manually?
If so, you’ve already got the steps figured out.


#8

If “solve the problems manually” means knowing what to write in the answers, I follow whatever instructions that are left in the ‘Get a hint’ sections of the course. I have been able to solve the most simplest of problems but ones like this one in discussion, I NEVER would have deduced what to do on my own.

This is an example of a problem that I did not know how to approach, until I clicked the Solution button.

def larger_sum(lst1, lst2):
sum1 = 0
sum2 = 0
for number in lst1:
sum1 += number
for number in lst2:
sum2 += number
if sum1 >= sum2:
return lst1
else:
return lst2

To me, the situation is like this: We all know how to operate a drill, an electrical saw, a hammer, screwdriver, and a wrench. Now, does that mean we have the knowledge to build a house, or a car, even with all of the materials included? After the foundation (in the case of programming, the def statement, followed by the parentheses and arguments) What comes next and when?

So I have all of the tools but none of the skills/experience as to when to apply which tool or when to use several tools in combination. As I asked earlier, how is everyone else able to solve the challenge problems if they have had no prior knowledge of programming?

Again, any response welcomed. Thanks.


#9

It means, given pen and paper, and the input, could you produce the looked-for output?
Or in your head for that matter.

You carry out a bunch of steps to do that.

A programming language provides ways for you to shuffle information around, same as what you did yourself.
So, what were the steps? Maybe you’d start by writing them down. You may need to break them down into smaller parts, ones that you know how to do in python.

I think you’ll find that if you put words to how the problems can be solved, that’ll be quite close to what you’d write in python.
There’s no human reading those words, you have to spell everything out in full detail.

If there’s some part of what you wrote that you don’t know how to express then you’re already a good way to finding it out, because at that point you’ve identified what information you need. It can probably be googled!

Arguably we all have a life-long experience of this information-wrangling.


#10
def over_nine_thousand(lst):
  lst_sum = 0
  if len(lst) != 0:
    while True:
      for i in lst:
        lst_sum = lst_sum + i
        if lst_sum > 9000:
          break
      return lst_sum
  return lst_sum
    

#11

You can remove your while-loop because it only does one iteration and is therefore the same as no loop at all.
And then you can remove your first if-statement too (empty list is not a special case)


#12

Hi,

I ended up with this solution, that was accepted:

def over_nine_thousand(lst):
power = 0
for i in lst:
while power < 9000:
power += i
break
return power

How can we solve using only for loop?~
Thanks


#13

That won’t run because it’s missing indentation which is part of the information that needs to exist in your code.
It looks like you have a completely unconditional break, and if so then one of your loops isn’t a loop at all because it’ll only run at most once. (That also makes me question if it’s doing the right thing overall)


#14

Hello ionatan,

Sorry, when i pasted the code in the forum message, all indentation was lost :slightly_smiling_face:
Here’s the code with the correct indentation:

def over_nine_thousand(lst):
  power = 0
  for i in lst:
    while power < 9000:
      power += i
    break
  return power

I used the break to stop the while loop as soon as the power variable gets over the 9000 thresold.
Oddly as it may seem, this solution was accepted by the editor and allowed me to move to the next lesson.

I also tried lots of different inputs and always got the expected output.


#16

Almost all forums have formatting tools, code should not be reformatted, so for that purpose one of the buttons in the post editor is for escaping those formatting rules.

A loop that executes once isn’t a loop. If your break is inside the while loop then that’s not a while loop it’s an if-statement. Did you know it only executes once? You need to be a bit more deliberate about your code because you’re the one who says what should happen.
If the break is in the for-loop, then only the first element in the list would get considered.

See how it’s very weird either way?

And, because you don’t actually use break (it’s just an if-statement), you changed the behaviour. That lets me construct some input where it fails - your loop won’t stop early (break), so if I give it an infinite series of numbers then your loop won’t ever stop:

from itertools import cycle

over_nine_thousand(cycle([1]))

cycle does exactly what its name suggests. It repeats values in an infinite cycle, so the input here is
[1,1,1,1,1,1, ...]
What the function should be doing is adding those 1’s up until it reaches 9001, at which point it should stop the loop and return the result.


#17

Hi again ionatan,

I understand your logic, and I replaced the while loop with an if statement and it works the same way :slight_smile:
Thank you for your time and for your tips. I actually edited my last post in order to represent the identation of the code :smiley:


#18

Ahh I hope your current code isn’t what’s in your post above, because that will only look at the first value.
(now the for-loop isn’t a loop instead)

Oh and your condition less than 9000 doesn’t match the instructions!

until the sum is greater than 9000

(you’re off by one, 9000 is not greater than 9000, but it causes your condition to evaluate to false)

Also, here’s an alternative which has the stopping condition in the loop header instead of using break:

def over_nine_thousand(numbers):
    numbers = iter(numbers)
    power = 0
    while power <= 9000:
        power += next(numbers)
    return power

iter returns an iterator which is a value which can be repeatedly asked for next value

…there’s a problem with that though, what if there aren’t enough numbers to exceed 9000? I suppose one might just say that’s undefined and it’s the caller’s fault if that happens.


#19
def over_nine_thousand(lst):
  total =[]
  for i in lst:
    total.append(i)                         #appending elements of i into total
    if sum(total) >= 9000:            #giving code a condition
      break                                       #breaking loop when it reaches approx. 9000
      return total                            #returning new element into total

I really thought this would work, but it returns None and I don't understand why. Can anyone help guide me in the right direction?

Thank you!

#20

Do you know what break and return do?
If no, go read up on that
If yes, read your code
Your use of them suggests no. You may of course not have thought it through, so that’s what the “yes” option there is suggesting you do.

Alternatively, add prints in your code which write out everything that’s being done and observe. You can also use this to compare what you expected to happen.


#21

I am familiar with return and break.
My idea was to break the loop and return the new list with the indicies sum to/around 9000.
I’ve been working on this code and reading different ideas on stack overflow to help guide me which might have confused my logic.


#22

break and return

Your return comes after break
What break does is precisely to prevent what follows from executing
If you want to return after breaking then you would need to put the return statement at the location where execution will continue after breaking, and additionally consider whether that’s also the right thing to do when execution reaches that location without breaking.

By the way, return has somewhat similar behaviour to break, so if you’re supposed to return, does it in any way help to break first?

And, in case you never break the loop, should that not also have a result? (Consider all paths your code may take)