求助:OsuLearn运行时出现ZeroDivisionError除零错误如何解决?
修复ZeroDivisionError除零错误
错误栈
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) Cell In[17], line 1 ----> 1 data_files = osulearn.dataset.all_files(OSU_FOLDER, verbose=True) 3 if not os.path.exists('.data'): 4 os.makedirs('.data') File c:\Users\danil\OneDrive\Desktop\OsuLearn-master\OsuLearn-master\osulearn\dataset.py:41, in all_files(osu_path, limit, verbose) 39 for i in range(len(replays)-1, -1, -1): 40 if verbose: ---> 41 _print_progress_bar(replays, i, reverse=True) 43 beatmap = _get_replay_beatmap_file(osu_path, replays[i]) 45 if beatmap is None: File c:\Users\danil\OneDrive\Desktop\OsuLearn-master\OsuLearn-master\osulearn\_cli.py:4, in _print_progress_bar(collection, index, bar_width, buffer_width, reverse) 1 def _print_progress_bar(collection, index, bar_width=40, buffer_width=80, reverse=False): 2 bar_format = "\r[{done}>{todo}] {text}" ----> 4 progress = index / (len(collection) - 1) 5 if reverse: 6 progress = 1 - progress ZeroDivisionError: division by zero
问题原因
当传入_print_progress_bar函数的collection(此处为replays列表)长度为1时,len(collection)-1等于0,触发除零错误。
修复方案
方案1:在进度条函数内部处理边界情况
修改osulearn/_cli.py中的_print_progress_bar函数,增加长度判断:
def _print_progress_bar(collection, index, bar_width=40, buffer_width=80, reverse=False): bar_format = "\r[{done}>{todo}] {text}" collection_len = len(collection) # 处理集合为空或仅含一个元素的情况 if collection_len <= 1: progress = 1.0 if reverse else 0.0 else: progress = index / (collection_len - 1) if reverse: progress = 1 - progress # 保留原函数后续的进度条绘制代码
方案2:在调用进度条函数前过滤无效场景
在osulearn/dataset.py的循环中,仅当replays列表长度大于1时才调用进度条函数:
for i in range(len(replays)-1, -1, -1): if verbose: if len(replays) > 1: _print_progress_bar(replays, i, reverse=True) # 保留原循环后续代码
内容的提问来源于stack exchange,提问作者Mirin's Core
相关产品推荐
相关产品推荐

