简体   繁体   English

python登录系统中的while循环不断重复并且不会中断

[英]While loop in python login system keeps repeating and wont break

My teacher gave me an assignment to make a login system for just the username part and he's given the code but it doesn't work properly as the while loop keeps repeating for the user to input their username when they already have and doesn't move onto the next part of code.我的老师给了我一个任务,让我只为用户名部分制作一个登录系统,他给了代码,但它无法正常工作,因为 while 循环不断重复,让用户在用户已经拥有并且不动时输入他们的用户名到下一部分代码。 I don't think the code is even reading the file or splitting the lines either.我认为代码甚至没有读取文件或拆分行。

I've tried putting in the break function in different places and changing indents of the code but I'm so lost.我试过在不同的地方放置 break 函数并更改代码的缩进,但我很迷茫。 I've also tried changing the variable "StudentDetails" to UserData (the name of the csv file) but it doesn't change anything.我还尝试将变量“StudentDetails”更改为 UserData(csv 文件的名称),但它没有改变任何内容。

#Login System
#First Name, Last Name, D.O.B, Email, Username, Password

UFound = False
UAttempts = 0 #Set to 0 tries to enter username
#Allow the yser to try login 3 times

while (UFound == False and UAttempts <3):
    UName = input("Please enter your username: ")
    UAttempts = UAttempts +1 #Has entered username once
    #Opens csv file and reads
myFile = open("UserData.csv","r")
for line in myFile:
    StudentDetails = line.split(",") #Splits line into csv parts
    if StudentDetails[4] == UName: #Username is in database
        UFound = True
myFile.close() #Close the data file

if UFound == True:
  print("Welcome to the quiz!")

else:
  print("There seems to be a problem with your details.")

Actual result: Please enter your username: Aiza11 Please enter your username: Aiza11 Please enter your username: Aiza11 There seems to be a problem with your details.实际结果: 请输入您的用户名:Aiza11 请输入您的用户名:Aiza11 请输入您的用户名:Aiza11 您的信息似乎有问题。

Aiza11 is a username in the csv file but it keeps asking for me to input username three times before saying it's incorrect... Aiza11 是 csv 文件中的用户名,但它一直要求我输入用户名三遍,然后说它不正确......

Your running into this problem because your not checking if its a valid username with your while loop.您遇到这个问题是因为您没有使用 while 循环检查它是否是有效的用户名。 This should all be in a while loop.这应该都在一个while循环中。 I would also open and close the file outside the loop so its not opening and closing the file every time.我也会在循环外打开和关闭文件,这样它就不会每次都打开和关闭文件。 I would also add a section to catch too many attempts.我还会添加一个部分来捕获太多尝试。 Doing that you can remove the and statement in the while loop.这样做您可以删除 while 循环中的 and 语句。

myFile = open("UserData.csv","r")
while UFound == False:
    UName = input("Please enter your username: ")
    UAttempts = UAttempts +1 #Has entered username once
    if UAttempts >2:
       print('Too many attempts')
       break
    #Opens csv file and reads
    myFile = open("UserData.csv","r")
    for line in myFile:
       StudentDetails = line.split(",") #Splits line into csv parts
       if StudentDetails[4] == UName: #Username is in database
           print("Welcome to the quiz!")
           UFound = True
    

     else:
        print("There seems to be a problem with your details.")
myFile.close() #Close the data file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM