-1

I have a line of Python code that doesn't work the way it should (at least to the best of my knowledge). Following is the code line:

print 'progress: {}%'.format((i/len(e_numbers))*100)

The value of i starts from 0 and goes up to length of e_numbers which is a list, while the length of e_numbers is around 17000. But the code always prints Progress: 0%.

Any idea why?

Bharata
  • 13,509
  • 6
  • 36
  • 50
rootkit
  • 353
  • 1
  • 3
  • 15

1 Answers1

2

In Python 2, using / to divide two integers performs integer division by default, rounding the result down to the nearest integer value. Thus, as long as i is between 0 and len(e_numbers), i/len(e_numbers) is going to be 0. There are two possible solutions:

  1. Cast one or both operands to a float before dividing, e.g., float(i)/len(e_numbers)

  2. Put from __future__ import division at the top of your file so that / always produces a float.

jwodder
  • 54,758
  • 12
  • 108
  • 124