What is wrong with this for loop?

for i in range(0, float(‘inf’)):
print(i)

What is wrong with this for loop? Why wont it run?

Hello Heat Jack,

Rather than asking what’s wrong with the loop, you should read the error

message, which will tell you what’s wrong with the loop.

TypeError: 'float' object cannot be interpreted as an integer

The range function requires integers, specifically ints, and does

not work with floats. You must give actual int values, not floats.

If you want an infinite loop that starts at zero and counts forever, you

can do this:

import itertools

for i in itertools.count():

    print(i)
1 Like

thank you Steven

I guess this thread is done
I have one more question that im working on

probably post it tomorrow or something

To print forever you can also do this:

while True:
  print(i) 

or

while 1:
  print(i)

This, as it is, is a terrible idea though as your computer will (almost certainly) eventually crash. You need some code condition to break out of the while loop.