Home > Enterprise >  For loop only reads the last line of the text file
For loop only reads the last line of the text file

Time:11-20

I have a simple login program where I need user to input the correct details to proceed ,This is my code:

email_l = []
pass_l = []
f = open("lab6.txt", "r")
content = f.readlines()
print(content)
for line in content:
    s = line.rstrip()
    name,email,password,cn,dob,citi,emergency,creditcardnum,creditcardexp,points = s.split(",")
    email_l = email
    pass_l = password
usergmail = input("enter gmail:")
if usergmail in email_l:
    passcode = input("enter password:")
    if passcode in pass_l:
        print("Login successful! Welcome",name)
        display_user()
    else:
        print("Wrong Password!")
else:
    print("wrong gmail")

and this is what contained in the text file

JunYing,[email protected],654321,0125489875,12/05/2001,Malaysian,0175987865,2546 4587 5895 5423,21/28,762
john,[email protected],123456,0165784399,17/7/2003,Malaysian,0124758995,5874 4585 4569 4214,09/25,547
Pepe,[email protected],123598,02654898,8/02/2011,American,02165897,5896 4578 5215 4512,07/25,541

I found out it only reads the last line of the file but I'm using a for loop shouldn't it be reading every line in the file? How can I make it to read every line in the file and make every email that entered into the input is matched with the file. Due to some rules, only Array can be utilized in the assignment so I can only use array

CodePudding user response:

You should use email_l.append(email) instead of just over writing it since it is a Python List.

CodePudding user response:

Your list variables "email_l" and "pass_l" are overwritten in each loop iteration.

you have to use:

email_l.append(email)

pass_l.append(password)

in order for your code to work.

refer to python data structures documentation to learn more

https://docs.python.org/3/tutorial/datastructures.html

Take care "display_user()" function is not defined in your communicated code. This will raise undefined function errors.

  • Related