0

So I am making this program where I let the user login in and I check their details(which are stored in an external file) and if they are wrong then I want the program to make them enter their details again. However with this code even if I enter the correct details it still says incorrect and loops. The code:

login_username = input("Enter your username: ")
login_password = input("Enter your password: ")
file_2 = open("data.txt","r")
for line in file_2:
    info = line.split(",")
    if login_username == info[0] and login_password == info[1]:
        quiz()

    while login_username != info[0] and login_password != info[1]:
        print("Error")
        login_username = input("Enter your username: ")
        login_password = input("Enter your password: ")

Any clue?

martineau
  • 119,623
  • 25
  • 170
  • 301
robxt
  • 11
  • Are you sure that info is getting the correct information and isn't just null? –  Nov 24 '17 at 17:30
  • what does `data.txt` contain? Username `\n` and password? Just `print(info[0])` and `info[1]` to check – Snow Nov 24 '17 at 17:31
  • Print out your `info` variable, I'm sure `info[1]` will have a newline at its end. – hoefling Nov 24 '17 at 17:32
  • it contains it in this order - username,password and i use csv writer to make them into arrays – robxt Nov 24 '17 at 17:33
  • You should check the whole file once, and check if there is occurrence of same username and password at least once. if there is no occurrence then only ask username and password again. i hope this will help. – Jay Joshi Nov 24 '17 at 17:34
  • I did that in another part of my program but it is this while loop that is a roadblock – robxt Nov 24 '17 at 17:35
  • the info part isn't wrong because when i remove the while loop it works – robxt Nov 24 '17 at 17:37
  • 1
    The reason your code is going in infinite loop, is once you get the password and username from first line, you straight away are stuck in the while loop, where in if you enter the login credentials in the data file, that doesn't come on the first line. I suggest that in the while loop, you add the for loop to check if the input matches any username and password combination of the file, and not just the login credentials on the first line. – sk1pro99 Nov 24 '17 at 17:44
  • @sk1pro99 how would i do that? – robxt Nov 24 '17 at 17:46
  • Try this: `login_username = input("Enter your username: ") login_password = input("Enter your password: ") file_2 = open("data.txt","r") while True: login_username = input("Enter your username: ") login_password = input("Enter your password: ") for line in file_2: info = line.split(",") if login_username == info[0] and login_password == info[1]: quiz() print("Error")` – sk1pro99 Nov 24 '17 at 18:18

0 Answers0