Reputation:
Below is a copy of my code. I keep getting a syntax error for elif centralLat and centralLong. I don't understand why. I indented everything correctly. The first if easternLat and easternLong works. Why don't the elif statements for centralLong and centralLat, etc work?
For this assignment, I have to check each latitude and longitude for a specific time zone and calculate the happiness score for a timezone. This happiness score is the sum of the sentiment values of tweets in a timezone divided by the number of tweets in a timezone.
Why do I keep getting an error?
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)
Upvotes: 0
Views: 335
Reputation: 59210
Let's take one small portion of your code.
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 ...
This is one if
statement (line 1), which contains a for loop (2).
Then after the if
statement are two lines (4 and 5).
Then there is the elif
statement (6).
Those lines (4 and 5) prevent the elif
from pairing up with the if
.
If those lines (4 and 5) are supposed to be part of your if
statement, they should be indented accordingly.
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 ...
This would produce a valid if/elif
structure.
Upvotes: 2