My professor wrote the second code snippet during a demo. Their output was a float. For me, only the first code snippet is a float. Any idea why there is a difference? The second code snippet seems to have integers divided by integers, added to integers so I'm not sure why it was converted to a float.
n = 4
total = 0
for i in range(n+1):
total = total + 1 / float(2**i)
return total
n = 4
total = 0
for i in range(n+1):
total = total + 1 / 2**i
return total
floatis the difference :-) what python version do you use?int/int=int. In Python 3,int/int=floatfrom __future__ import division:-)