I have to create a pagination function asking for 2 arguments, an array and a page_number.
I was trying to solve for any no of item's in a list I am stuck into some problem.
Here is my code:
def paginator(array,page_number):
if 0 < page_number < len(array):
obj_per_page = len(array)//page_number
objects = len(array)
count = 0
start = 0
while objects > 1:
count += 1
objects -= len(array)//page_number
object_available = array[start:obj_per_page]
print(f'this is page {count} have object {object_available}')
start = obj_per_page
obj_per_page += len(array)//page_number
elif len(array) <= page_number:
for i in array:
print(f'thi is page {i} have object [{i}]')
else:
print(array)
list = [1,2,3,4,5,6]
page_number = 3
paginator(list,page_number)
output
this is page 1 have object [1, 2]
this is page 2 have object [3, 4]
this is page 3 have object [5, 6]
`this code provide expected output only when
if len(list) is divisible by page_number if page_number is 0 or 1 if page_number is greater and equal to len(list)`
Bugs
let's say len(list) is 9 and page_number are either of (2,4,5,6,7,8) the remainder of 9 % (2,4,5,6,7,8) is (1,1,4,3,2,1) ,so the number of item's are missing equal to remainder
what i want
if len(list) is 9 and page_number is 4 and I want output something like
this is page 1 have object [1, 2, 3]
this is page 2 have object [4, 5]
this is page 3 have object [6, 7]
this is page 4 have object [8, 9]
Help me if you guys have other solution share it I try to explain my problem sorry for bad grammer