Why do I get an infinite loop?

Question

Why do I get an infinite loop?

Answer

If you’ve incremented num outside of the while loop, then it will never be reached. When a while loop is entered, it only runs the code indented to be inside of it each time it loops. This means that if you incremented num but didn’t indent it to be in the loop, it never gets executed when the loop runs, and our while condition never becomes false!

1 Like

Getting caught in an infinite loop means you cannot reset the interface so it’s impossible to finish the lesson.

4 Likes

you can refresh the web page? At least, within codecademy you can

Catching the same problem. Even logout/login or clearing local storage + clearing cache + hard reboot doesn’t help

1 Like

So what did you do instead

why don’t you guys close out of the tab? and search up “codecademy” on google? never thought of that!?

3 Likes

It happened to me also, but I refreshed the page and it came back just fine.

1 Like

I could use some help (I don’t want the answer. I want to figure it out), but every time I run my code I get an infinite loop. I read the comment at the top but I guess I’m still not getting it.

Here is what I have:

num = 1

while num <= 10:
  print num ** 2
num = False
1 Like

There are several problems, for starters, the exponent of 1 is 1. You can see this from your print statement. So you would need to do different math, otherwise num is never going to get 10

also, what do you do with the result of your math operation? You only print it, the num variable never gets updated

2 Likes

Thanks! I finally figured out it. I really appreciate it.

On a mac you can force quit safari and re-open it to change the code

num = 1

while num <= 10:

print (num ** 2)

num += 1

correct answer I had a lot of issues at first though