#5.随意写一个20行以上的文件(divmod) # 运行程序,先将内容读到内存中,用列表存储。 # l = [] # 提示:一共有多少页 # 接收用户输入页码,每页5条,仅输出当页的内容 def read_page(bk_list,n,endline=None): startline = 5 * (n-1) endline = endline+startline-1 if endline else startline + 4 # print(startline,endline) for i in range(startline,endline+1): print(bk_list[i],end = '') def read_book(filename): f = open(filename) book_list = f.readlines() f.close() return book_list book_list = read_book('tmp_file') line_num = len(book_list) x,y = divmod(line_num,5) # if y: # page = x+1 # else: # page = x page = x+1 if y else x print('一共有%s页'%page) while True: page_num = input('请输入您要阅读的页码 : ').strip() if page_num.isdigit(): page_num = int(page_num) if page_num < page: read_page(book_list,page_num) elif page_num == page: read_page(book_list,page_num,y) else: print('您输入的内容有误')