如何让Python函数基于start参数从指定章节计算剩余阅读页数?
修正Python阅读进度计算函数,从指定章节开始统计
我有一个存储书籍季、章节名称及对应页数的嵌套字典:
text = { 1: { 1: {"ch.name": "The Boy Who Lived", "pages": "146"}, 2: {"ch.name": "The Vanishing Glass", "pages": "126"}, }, 2: { 1: {"ch.name": "The Worst Birthday", "pages": "129"}, 2: {"ch.name": "Dobby's Warning", "pages": "125"}, }, }
我编写了stranger_binger函数用于计算剩余可阅读页数并输出进度,但调用时传入start=(1,2)(从第1季第2章开始)、pages_left=300,结果没有从指定章节开始统计,而是从第1季第1章开始了。
原函数代码:
def stranger_binger(seasons,start,pages_left): num_pages_watched = 0 for key1, value1 in seasons.items(): for key2, value2 in value1.items(): output = key1,key2,value2['ch.name'],value2['pages'] output for key1, value1 in seasons.items(): for key2, value2 in value1.items(): pages_left = pages_left-int(value2['pages']) if start == start and pages_left > 0: pages_left if start == start and pages_left > 0: left = pages_left print(f"Watched Season {key1} Chapter {key2}: '{value2['ch.name']}', {pages_left} pages left !") num_pages_watched = num_pages_watched +1 elif start == start and pages_left < 0: print(f"No more full Chapter today! Only {left} pages are left , but Season {key1} Chapter {key2}: '{value2['ch.name']}' has {value2['pages']}pages.") print(f"{num_pages_watched} chapter(s) watched today.") return start stranger_binger({ 1: { 1: {"ch.name": "The Boy Who Lived", "pages": "146"}, 2: {"ch.name": "The Vanishing Glass", "pages": "126"}, }, 2: { 1: {"ch.name": "The Worst Birthday", "pages": "129"}, 2: {"ch.name": "Dobby's Warning", "pages": "125"}, }, },(1,2),300)
当前错误输出:
Watched Season 1 Chapter 1: 'The Boy Who Lived', 154 pages left ! Watched Season 1 Chapter 2: 'The Vanishing Glass', 28 pages left ! No more full Chapter today! Only 28 pages are left , but Season 2 Chapter 1: 'The Worst Birthday' has 129pages. 2 chapter(s) watched today.
预期输出:
Watched Season 1 Chapter 2: 'The Vanishing Glass', 174 pages left ! Watched Season 2 Chapter 1: 'The Worst Birthday', 45 pages left ! No more full Chapter today! Only 45 pages are left , but Season 2 Chapter 2: 'Dobby's Warning' has 125pages. 2 chapter(s) watched today.
修正后的函数代码
def stranger_binger(seasons, start, pages_left): num_pages_watched = 0 start_season, start_chapter = start started = False # 标记是否到达开始章节 # 按季的数字顺序遍历(兼容Python3.7以下无序字典的情况) for season_num in sorted(seasons.keys()): chapters = seasons[season_num] # 按章节数字顺序遍历 for chapter_num in sorted(chapters.keys()): chapter_info = chapters[chapter_num] chapter_pages = int(chapter_info['pages']) # 未到达起始章节时跳过统计 if not started: if season_num == start_season and chapter_num == start_chapter: started = True else: continue # 判断剩余页数是否足够阅读当前章节 if pages_left >= chapter_pages: pages_left -= chapter_pages num_pages_watched += 1 print(f"Watched Season {season_num} Chapter {chapter_num}: '{chapter_info['ch.name']}', {pages_left} pages left !") else: print(f"No more full Chapter today! Only {pages_left} pages are left , but Season {season_num} Chapter {chapter_num}: '{chapter_info['ch.name']}' has {chapter_pages} pages.") print(f"{num_pages_watched} chapter(s) watched today.") return start stranger_binger({ 1: { 1: {"ch.name": "The Boy Who Lived", "pages": "146"}, 2: {"ch.name": "The Vanishing Glass", "pages": "126"}, }, 2: { 1: {"ch.name": "The Worst Birthday", "pages": "129"}, 2: {"ch.name": "Dobby's Warning", "pages": "125"}, }, }, (1,2), 300)
修改说明
- 添加起始标记:新增
started变量,用来判断是否到达指定的起始章节,未到达时跳过所有统计逻辑。 - 有序遍历章节:用
sorted()处理季和章节的键,确保遍历顺序是数字从小到大,避免Python3.7以下版本字典无序导致的遍历顺序错误。 - 修正页数计算逻辑:原函数先直接扣减页数再判断剩余量,改为先判断剩余页数是否足够读当前章节,再执行扣减,逻辑更严谨。
- 移除无效代码:删除原函数中无意义的
output变量赋值和空的if start == start判断语句。
内容的提问来源于stack exchange,提问作者Talkthattalk
相关产品推荐
相关产品推荐

