gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:20:11 24 4
gpt4 key购买 nike

下面是我的代码的副本。我一直收到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)

最佳答案

让我们拿一小部分代码。

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结构。

关于python - 为什么我的elif语句出现语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55282096/

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