为什么我的elif语句出现语法错误?

时间:2019-03-21 13:57:32

标签: python if-statement syntax-error

下面是我的代码的副本。我不断收到elif centralLat和centralLong的语法错误。我不明白为什么。我正确地缩进了一切。首先是EasternLat和EasternLong的作品。为什么centralLong和centralLat等的elif语句不起作用?

对于此作业,我必须检查特定时区的每个经度和纬度,并计算该时区的幸福感得分。该幸福分数是时区中推文的情感值的总和除以时区中推文的数量。

为什么我总是出错?

for line in infile2:
        line=line.rstrip()
        words=line.split()
        firststriplat=words[0].rstrip(",")
        lat=firststriplat.lstrip("[")
        lat=float(lat)
        long=words[1].rstrip("]")
        long=float(long)
        easternLat= 24.660845 <= lat and lat<=49.189787
        easternLong= -87.518395 <= long <= -67.444574
        centralLat= 24.660845 <= lat and lat<=49.189787
        centralLong= -101.998892 <= long <= -87.518395
        mountainLat=24.660845 <= lat and lat<=49.189787
        mountainLong=-115.236428 <= long <= -101.998892
        pacificLat=24.660845 <= lat and lat<=49.189787
        pacificLong= -125.242264<= long <= -115.236428
        if easternLat and easternLong:
            for word in words:
                if word in depressed:
                    depressedKeys=depressedKeys+1
                elif word in okay:
                    okayKeys=okayKeys+1
                elif word in good:
                    goodKeys=goodKeys+1
                elif word in happy:
                    happyKeys=happyKeys+1
                else:
                    pass
        numOfTweetsEastern=numOfTweetsEastern+1
        sentimentValueEastern=(depressedKeys*DEPRESSEDVALUE)+(okayKeys*OKAYVALUE)+(goodKeys*GOODVALUE)+(happyKeys*HAPPYVALUE)
        elif centralLat and centralLong:
            for word in words:
                if word in depressed:
                    depressedKeys=depressedKeys+1
                elif word in okay:
                    okayKeys=okayKeys+1
                elif word in good:
                    goodKeys=goodKeys+1
                elif word in happy:
                    happyKeys=happyKeys+1
                else:
                    pass
        numOfTweetsCentral=numOfTweetsCentral+1
        sentimentValueCentral=(depressedKeys*DEPRESSEDVALUE)+(okayKeys*OKAYVALUE)+(goodKeys*GOODVALUE)+(happyKeys*HAPPYVALUE)
        elif mountainLat and mountainLong:
             for word in words:
                if word in depressed:
                    depressedKeys=depressedKeys+1
                elif word in okay:
                    okayKeys=okayKeys+1
                elif word in good:
                    goodKeys=goodKeys+1
                elif word in happy:
                    happyKeys=happyKeys+1
                else:
                    pass
        numOfTweetsMountain=numOfTweetsMountain+1
        sentimentValueMountain=(depressedKeys*DEPRESSEDVALUE)+(okayKeys*OKAYVALUE)+(goodKeys*GOODVALUE)+(happyKeys*HAPPYVALUE)
        elif pacificLat and pacificLong:
            for word in words:
                if word in depressed:
                    depressedKeys=depressedKeys+1
                elif word in okay:
                    okayKeys=okayKeys+1
                elif word in good:
                    goodKeys=goodKeys+1
                elif word in happy:
                    happyKeys=happyKeys+1
                else:
                    pass
        numOfTweetsPacific=numOfTweetsPacific+1
        sentimentValuePacific=(depressedKeys*DEPRESSEDVALUE)+(okayKeys*OKAYVALUE)+(goodKeys*GOODVALUE)+(happyKeys*HAPPYVALUE)
        else:
            pass
    happScoreEastern=sentimentValueEastern/numOfTweetsEastern
    happScoreCentral=sentimentValueCentral/numOfTweetsCentral
    happScoreMountain=sentimentValueMountain/numOfTweetsMountain
    happScorePacific=sentimentValuePacific/numOfTweetsPacific
    print(happScoreEastern)
    print(happScoreCentral)
    print(happScoreMountain)
    print(happScorePacific)

2 个答案:

答案 0 :(得分:2)

让我们取一小部分代码。

1    if easternLat and easternLong:
2        for word in words:
3            ...
4    numOfTweetsEastern=numOfTweetsEastern+1
5    sentimentValueEastern=(depressedKeys*DEPRESSEDVALUE)+(okayKeys*OKAYVALUE)+(goodKeys*GOODVALUE)+(happyKeys*HAPPYVALUE)
6    elif centralLat and centralLong:
7        for word in words:
8            ...

这是一条if语句(第1行),其中包含一个for循环(2)。
然后,在if语句的之后是两行(4和5)。
然后是elif语句(6)。

这些行(第4和第5行)阻止elifif配对。

如果这些行(4和5)应该属于您的if语句的一部分,则应相应地对其进行缩进。

1    if easternLat and easternLong:
2        for word in words:
3            ...
4        numOfTweetsEastern=numOfTweetsEastern+1
5        sentimentValueEastern=(depressedKeys*DEPRESSEDVALUE)+(okayKeys*OKAYVALUE)+(goodKeys*GOODVALUE)+(happyKeys*HAPPYVALUE)
6    elif centralLat and centralLong:
7        for word in words:
8            ...

这将产生有效的if/elif结构。

答案 1 :(得分:0)

要添加到已经说过的话中:

  • 您的for循环不必要地重复了多次
  • else: pass不执行任何操作,可以将其删除
  • 您无需在循环之间重置计数器-每次都需要重置为零
  • 您没有将情感值添加到例如“ sentimentValueMountain”中,因此在该时区的1条以上记录中将是错误的

因此将其应用于代码中的中心逻辑:

    ...
    pacificLong= -125.242264<= long <= -115.236428

    # You need to set all of your counters to zero here 

    for word in words: # do this once, then add the result to the relevant timezone
        if word in depressed:
            depressedKeys += 1
        elif word in okay:
            okayKeys += 1
        elif word in good:
            goodKeys += 1
        elif word in happy:
            happyKeys += 1   
    sentimentValue = (depressedKeys*DEPRESSEDVALUE)+(okayKeys*OKAYVALUE) + (goodKeys*GOODVALUE) + (happyKeys*HAPPYVALUE)
    if easternLat and easternLong:
        numOfTweetsEastern += 1
        sentimentValueEastern += sentimentValue # changed = to +=
    elif centralLat and centralLong:
        numOfTweetsCentral += 1
        sentimentValueCentral += sentimentValue
    elif mountainLat and mountainLong:
        numOfTweetsMountain += 1
        sentimentValueMountain += sentimentValue
    elif pacificLat and pacificLong:
        numOfTweetsPacific += 1
        sentimentValuePacific += sentimentValue

happScoreEastern=sentimentValueEastern/numOfTweetsEastern
...