2

I'm doing some stuff on several lines of data. This takes a long time, and I would like to show the percentage of progress.

So I have the following code:

for y in range(0, height):
    if (y * 100 / height).is_integer():
        print("... ", int(y * 100 / height), "%")

height is the number of lines that need to be processed.

However, somehow this code doesn't print the correct percentages. If height is 100 it works fine. For 4050 it prints every 2 percentages (0%, 2%, 4%, ...). For 2025 it prints every 4 percentages...

Why does this happen? And how can I fix it?

The Oddler
  • 6,314
  • 7
  • 51
  • 94

1 Answers1

2

Not exactly proud of my code, but anyway:

last = -1 # Start it as -1, so that it still prints 0%.
for y in range(0, height):
    work = int(y * 100 / height) # I assigned the percentage to a variable for neatness.
    if work != last: # so it doesn't print the same percent over and over.
        print("... ", work, "%")
    last = work # Reset 'last'.

This may/may not be completely accurate. But it works

The reason you had your problem is from the is_integer() was only true on specific values.

Hope this helps!

Coolq B
  • 344
  • 5
  • 10