mardi 4 août 2020

Python reading csv and executing print statement multiple times

I have a csv file with 4 fields:

JFK,John F Kennedy International,5326,5486 
ORY,Paris-Orly,629,379
MAD,Adolfo Suarez Madrid-Barajas,1428,1151
AMS,Amsterdam Schiphol,526,489
CAI,Cairo International,3779,3584

I compare the code entered by the user with the first field of each row. If it matches, I want to print the second field. If it doesn't match with any entry in the CSV, i want to print 'ERROR'.

But 'ERROR' gets printed multiple times even when the code is found.

How can I prevent that?

The code is:

if num == 1:
    Adetails = input("Enter airport details (Please enter the three letter code for the UK airport): ")
    while Adetails not in ("LPL", "BOH"):
        Adetails = input("Error three letter airport code invalid. Please enter either LPL or BOH: ")
    Odetails = input("Enter overseas airport details (Please enter the three letter code for the overseas airport): ")
    for row in inputfile:
        if row[0] == Odetails:
            print(row[1])
            repeat = False
        else:
            if row[0] != Odetails:
                print("ERROR")

Aucun commentaire:

Enregistrer un commentaire