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

2024-06-25 22:31:44 发布

您现在位置: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)

Tags: csvinemail错误rowappendfirstnamestheater
1条回答
网友
1楼 · 发布于 2024-06-25 22:31:44

当您试图查找列表中不存在项的索引时,会得到一个ValueError。有两种处理方法那个。你呢可以将Emails1.index(Email)放入
^{} ^{},但是测试Email是否在Emails1中更简单。你知道吗

下面是代码的一个改进版本,它应该做你想做的事情。请注意,我使用'rb'模式打开CSV文件,正如the docs中建议的那样,尽管在python3中这并不是严格必要的。你知道吗

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]

相关问题 更多 >