# 非函数式: data_list = [] for i in range(1,901): data_list.append('豆谷云-%s' % i) while True: # 1.你要查看的页码 page = int(input("请输入你要查看的页码:")) # 2.每页显示多少条数据 per_page_num = 10 # 3.定义每页开始位置和结束位置 start = (page - 1) * per_page_num end = page * per_page_num page_data_list = data_list[start:end] for item in page_data_list: print(item)
# 面向对象: class Pagenation(object): def __init__(self,data_list,page,per_data_num = 10): ''' 初始化 :param data_list: 所有数据列表 :param page: 当前要查看的列表页 :param per_data_num: 每页默认要显示几条 ''' self.data_list = data_list self.page = page self.per_data_num = per_data_num @property def start(self): ''' 计算引索的起始位置 :return: ''' return (self.page - 1) * self.per_data_num @property def end(self): ''' 计算引索的结束位置 :return: ''' return self.page * self.per_data_num def show(self): ''' 切片取数据,展示对应分页的结果 :return: ''' result = self.data_list[self.start:self.end] for row in result: print(row) data_list = [] for i in range(1,901): data_list.append('豆谷云-%s' % i) while True: # 1.输入要查看的页码 page = int(input("请输入要查看的页码:")) obj = Pagenation(data_list,page) obj.show()