每当电子邮件输入错误时,Python文件处理错误

时间:2015-12-16 10:49:21

标签: python

每当输入密码并且输入密码时,它会显示“不正确的尝试次数”。但是,当我的电子邮件出错时,它会给我一条错误消息,而不是密码错误。我尝试为电子邮件构建另一个if循环但是没有用。

错误是:

Traceback (most recent call last):
File "E:\Theater.py", line 126, in <module>
TheaterFileLogin()
File "E:\Theater.py", line 56, in TheaterFileLogin
Emaildex = Emails1.index(Email)
ValueError: 's,dm' is not in list

代码是:

import csv
x=0
while x<3:
    with open('Theater.csv', 'r') as csvfile:
        Theater = csv.reader(csvfile, delimiter=",")
        Emails1 = []
        Passwords1 = []
        Passwords2 = []
        Passwords3 = []
        Passwords4 = []
        Firstnames = []
        Surnames = []
        for row in Theater:
            Firstname = row [1]
            Surname = row [0]
            Email1 = row[2]
            Password1 = row[7]
            Password2 = row[8]
            Password3 = row[9]
            Password4 = row[10]
            Firstnames.append(Firstname)
            Surnames.append(Surname)
            Emails1.append(Email1)
            Passwords1.append(Password1)
            Passwords2.append(Password2)
            Passwords3.append(Password3)
            Passwords4.append(Password4)
        Email = input("Email Adress: ")
        Password = input("Password: ")
        Emaildex = Emails1.index(Email)
        thepassword = Passwords1[Emaildex]
        adminlevel1password = Passwords2[Emaildex]
        adminlevel2password = Passwords3[Emaildex]
        adminlevel3password = Passwords4[Emaildex]
        FN = Firstnames[Emaildex]
        SN = Surnames[Emaildex]
        if thepassword == Password:
            print("Welcome")
            x=5
        else:
            print("Incorect email or password try again")
            x+=1
            print("Attempt",x)

1 个答案:

答案 0 :(得分:2)

当您尝试查找列表中不存在的项目的索引时,会得到ValueError。有两种方法可以解决这个问题。你可以将Emails1.index(Email)放在一个中 try: except ValueError:,阻止但只是测试Email是否在Emails1中更简单。

以下是代码的改进版本,执行您想要的操作。请注意,我按the docs中的建议打开了'rb'模式的CSV文件,尽管这在Python 3中并不是绝对必要的。

import csv

with open('Theater.csv', 'rb') as csvfile:
    Theater = csv.reader(csvfile, delimiter=",")
    Emails1 = []
    Passwords1 = []
    Passwords2 = []
    Passwords3 = []
    Passwords4 = []
    Firstnames = []
    Surnames = []
    for row in Theater:
        Firstname = row [1]
        Surname = row [0]
        Email1 = row[2]
        Password1 = row[7]
        Password2 = row[8]
        Password3 = row[9]
        Password4 = row[10]
        Firstnames.append(Firstname)
        Surnames.append(Surname)
        Emails1.append(Email1)
        Passwords1.append(Password1)
        Passwords2.append(Password2)
        Passwords3.append(Password3)
        Passwords4.append(Password4)

for attempt in range(1, 4):
    Email = input("Email Address: ")
    Password = input("Password: ")
    if Email in Emails1:
        Emaildex = Emails1.index(Email)
        thepassword = Passwords1[Emaildex]
        if thepassword == Password:
            break
    print("Incorrect email or password.")
    if attempt < 3:
        print("Please try again.")
    print("Attempt", attempt)
else:
    print("Aborting.")
    exit()

print("Welcome")

adminlevel1password = Passwords2[Emaildex]
adminlevel2password = Passwords3[Emaildex]
adminlevel3password = Passwords4[Emaildex]
FN = Firstnames[Emaildex]
SN = Surnames[Emaildex]