mercredi 29 avril 2015

List index out of range parsing CSV file

For my school assignment I have created a program which checks for an EMAIL and PASSWORD found in rows 0 and 1 in a csv file which match up, and will then print some information about that user from rows 2-6. However, I believe this is not necessarily any cause of my problem.

The problem is the else statement.

When a user enters both their EMAIL and PASSWORD correctly the system will print their information. However if the EMAIl and PASSWORD entered don't match up with the ones in the CSV file, I attempted to create an ELSE statement that would print 'Your email or password is incorrect'

However, instead, I would receive this error:

line 12, in <module>
    if email_1 in row[0] and pass_1 in row[1]:
IndexError: list index out of range

Since I'm new to python, I'm not sure why this is happening, but I don't reckon it is to do with the code above since when details are entered correctly, the program will print the information.

I have tried to make the ELSE statement work without the use of assigning a variable to be TRUE or FALSE, but nor did that work, exactly the same issue. It's just the ELSE statement which it doesn't seem to be recognising.

Code:

import csv

email_pass = False
email_1 = raw_input('What is your email? :')
pass_1 = raw_input('What is your password? :')
with open('DATAFILE.csv', "rb") as csvfile:
    my_content = csv.reader(csvfile, delimiter=',')
    for row in my_content:
        if email_1 in row[0] and pass_1 in row[1]:
            email_pass = True
            if email_pass == True:
                print row[2:6]
                break
            else:
                print 'Your EMAIL or PASSWORD is incorrect'

Aucun commentaire:

Enregistrer un commentaire