以下是我的代码的副本。我一直为Elif Centrallat和Centrallong遇到语法错误。我不明白为什么。我正确地缩进了一切。第一个If Easternlat和Easternong起作用。为什么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 loop(2)。
然后 if
语句是两行(4和5)。
然后是elif
语句(6)。
这些行(4和5)阻止elif
与if
配对。
如果这些行(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
结构。