欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python 简单分页思路

程序员文章站 2022-11-30 15:58:52
一: 二: ......

一:

li = []
for i in range(1000):
    li.append(i)

while True:
    p = input('input page: ')
    p = int(p)

    start = (p-1) * 10
    end = p * 10
    print(li[start:end])

运行结果:
input page: 1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
input page: 2
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
input page: 5
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
input page: 

二:

class Pagegenerate:

    def  __init__(self, current_page):
        try:
            p = int(current_page)
        except Exception as e:
            p = 1

        self.page = int(current_page)

    @property
    def start(self):
        val = (self.page-1) * 10
        return  val

    @property
    def end(self):
        val = self.page * 10
        return val

li = []

for i in range(1000):
    li.append(i)

while True:
    p = input("Enter the page to view: ")
    obj = Pagegenerate(p)
    print(li[obj.start:obj.end])


运行结果:
Enter the page to view: 2
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Enter the page to view: 5
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]