technical problem on python with infinite float

Timothée Tardy :

I am using Python, and I have a problem, I want to do a program tha can count from 1 to infinite, to know how much is the infinite. Here is my code :

a=0
for i in range(1, 10e+99):
  a += 1
  print (a)

but it says " 'float' object cannot be interpreted as an integer "

whereas 10e+99 is not a float help me please

Riccardo :

The problem arises because the range() function takes an int, whereas 10e+99 is indeed a float. While 10e+99 is of course not infinity, and therefore you shouldn't expect infinity to pop up anywhere during the execution of your program, if you really wanted to get your for loop to work as it is you could simply do

a=0
for i in range(1, int(10e+99)):
    a += 1
    print (a)

As other users have pointed out, I would however rethink your strategy entirely: using a range-based for loop to "find out" the value of infinity just doesn't work. Infinity is not a number.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=220504&siteId=1