Simple forloop - 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?
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()
range takes a 3rd optional parameter of step.
So
range(0,234,100)
Gives
[0, 100, 200]
So you can do something like
for items in range(0,234,100):
get_next_100()
精彩评论