【问题标题】:Python: Why am I getting a "ZeroDivisionError: division by zero" in function?Python:为什么我在函数中得到“ZeroDivisionError:除以零”?
【发布时间】:2017-11-15 16:39:49
【问题描述】:

我的目标是计算文件中属于特定时区的推文总数。 我有以下函数(我已经用 cmets 注意到函数末尾附近的问题区域):

def readTweets(inFile, wordsName):    
words = []
lat = 0
long = 0
keyword = keywords(wordsName)
sents = keywordSentiment(wordsName)
value = 0
eastern = 0
central = 0
mountain = 0
pacific = 0
a = 0
b = 0
c = 0
d = 0
easternTweets = 0
centralTweets = 0
mountainTweets = 0
pacificTweets = 0

for line in inFile:
    entry = line.split()    

    for n in range(0, len(entry) - 1):
        entry[n] = entry[n].strip("[],!?#./-=+_@")
        if n > 4:   # n>4 because words begin on 5th index of list
            entry[n] = entry[n].lower()
            words.append(entry[n])

    lat = float(entry[0])
    long = float(entry[1])

    timezone = getTimeZone(lat, long)  
    if timezone == "eastern":
        easternTweets += 1
    if timezone == "central":
        centralTweets += 1
    if timezone == "mountain":
        mountainTweets += 1
    if timezone == "pacific":
        pacificTweets += 1

    for i in range(0, len(words)):
        for k in range(0, len(keyword)):
            if words[i] == keyword[k]:
                value = int(sents[k])
                if timezone == "eastern":
                    eastern += value
                    a += 1
                if timezone == "central":
                    central += value
                    b += 1
                if timezone == "mountain":
                    mountain += value
                    c += 1
                if timezone == "pacific":
                    pacific += value
                    d += 1

# the values of a,b,c,d are 0
easternTotal = eastern/a    # getting error 
centralTotal = central/b    # for 
mountainTotal = mountain/c  # these 
pacificTotal = pacific/d    # values

print("Total tweets per time zone:")
print("Eastern: %d" % easternTweets)
print("Central: %d" % centralTweets)
print("Mountain: %d" % mountainTweets)
print("Pacific: %d" % pacificTweets)

easternTotal 和使用 abcd 进行除法的其他总值出现 ZeroDivisionError: division by zero 错误。

如果我打印abcd 的值,则会显示0。我的问题是为什么它们的值是 0? abcd 的值在 if 语句中没有变化吗?

【问题讨论】:

  • 如果inFile 为空,或者一行不包含latlong,当传递给getTimeZone 时返回'eastern',那么a 将是0 .阅读如何创建minimal reproducible example
  • 请修正你的缩进并显示你是如何调用函数的。

标签: python python-3.x pycharm


【解决方案1】:

所以发生这种情况的唯一方法是因为永远不会到达递增 a、b、c 和 d 的代码。

这可能有几个原因:

  • inFile 是空的,所以整个 for 循环永远不会进入它的主体
  • len(words) 为 0,因此 for 循环永远不会进入其主体
  • len(keywords) 为 0,因此 for 循环永远不会进入其主体
  • timezone 的值与您测试的值不同

words 最初是 [],因此如果附加内容的循环永远不会运行,它的长度可以保持为 0。

从这里开始,我们不可能看到发生了哪些情况,但是对于您来说应该很容易通过一些打印语句等。

【讨论】:

    【解决方案2】:

    你将eastern 除以0。你可以通过这样做来避免它

    easternTotal = eastern/a if a > 0 else eastern
    

    【讨论】:

      【解决方案3】:

      因为你设置了 a,b,c,d=0; 当 readTweets(inFile, wordsName) 没有得到任何数据时,“eastern/a”可能会导致“eastern/0”。

      因此,请确保您的 readTweets() 确实首先获取了数据。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-03
        相关资源
        最近更新 更多