mercredi 12 octobre 2016

Python IF ELIF == statement not working as expected

I'm trying to look through multiple sources to see which one has a specific entry.

My input looks like:

json_str2 = urllib2.urlopen(URL2).read()
u = json.loads(json_str2)
#load all five results
One = u['flightStatuses'][0]
Two = u['flightStatuses'][1]
Three = u['flightStatuses'][2]
Four = u['flightStatuses'][3]
Five = u['flightStatuses'][4]

My IF statement works correctly like this: and I get the correct result

if One['flightNumber'] == str(FlEnd):
    fnumber = One['flightId']
    print fnumber

But when I add the elif: the answer is wrong (nothing) even when the first statement is true.

I don't understand why the elif doesn't work?

# if statement checks each result to define flightId, needed for tracking.
if One['flightNumber'] == str(FlEnd):
    fnumber = One['flightId']
elif Two['flightNumber'] == str(FlEnd):
    fnumber = Two['flightId']
print fnumber

Aucun commentaire:

Enregistrer un commentaire