Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple forloop - Python

Tags:

python

this is probably too simple of a question, but here I go.

I have paginated items, each page contains 100 items. The program fetches items till it reaches the item index specified within item_num

This is what I have:

item_num = 56

range(0, item_num/100 + (item_num%100 > 0)):
  get_next_100()

I'm not really sure about the (item_num%100 > 0) boolean I used.

Is there anything wrong with what I did?

like image 425
RadiantHex Avatar asked Mar 06 '26 23:03

RadiantHex


1 Answers

You seem to be trying to call the function zero times if item_num is 0, once if item_num is 1 to 100, twice if item_num is between 101 and 200, etc...

A simpler way to write this is:

n = 0
while n < item_num:
   get_next_100()
   n += 100

Or you could do it as a for loop:

for _ in range(0, item_num, 100):
   get_next_100()
like image 56
Mark Byers Avatar answered Mar 09 '26 12:03

Mark Byers