gpt4 book ai didi

python - 我收到错误 ZeroDivisionError : integer division or modulo by zero. 我的代码有什么问题

转载 作者:行者123 更新时间:2023-12-01 05:52:23 25 4
gpt4 key购买 nike

for i,j in enumerate(output.readlines()):
if i%len(tag) == 0:
[1]
[2]
etc
if i%len(tag) == len(tag) - 1:
outputfile.write(<"something">)

还有更多代码,但自从我编辑了此代码的前 2 行和最后 2 行以来,我一直遇到 ZeroDivisionError 问题。我不知道为什么会出现这个错误。

最佳答案

在尝试使用 len(tag) 来 mod(%) 另一个值之前,您需要检查并确保 len(tag) 不为 0。 mod(%) 基本上是除法运算,因此如果 len(tag) 等于 0,您将得到 ZeroDivisionError

考虑以下代码,如果 len(tag) 等于 0,它将转义 for 循环:

for i,j in enumerate(output.readlines()):
if len(tag) == 0:
continue
elif i % len(tag) == 0:
#Rest of your code here..
if i % len(tag) == len(tag) - 1:
outputfile.write("something")

关于python - 我收到错误 ZeroDivisionError : integer division or modulo by zero. 我的代码有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13715134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com