Search code examples
pythonfunctionfiletext

Why does one of my login functions work and the other does not, despite the fact that they both have the identical code?


So I am trying a login system for my bank management project and I created two of the login system. one for the admin system and the other for the customer. This is the code and text file for both. Why would my first function work and not the second? FYI I can't use any global function and dictionary and the error I been getting is ValueError: too many values to unpack (expected 2)

def LAdminAccount():
EnteredStaffAccountNumber = str(input("========== Please Type in Your Account Number:"))
EnteredStaffPassword = str(input("========== Please Type in Your Password:"))
A= open("AdminUser.txt","r")
for line in A.readlines():
    us,pw = line.strip().split("|")
    if (EnteredStaffAccountNumber == us ) and (EnteredStaffPassword == pw):
        print ("Login successful")
        A.close()
        AdminMenu()
print ("Wrong username/password")
return
def LCustomerAccount():
EnteredID = str(input("========== Please Type in Your Account ID:"))
EnteredPassword = str(input("========== Please Type in Your Password:"))
B= open("Customerlogin.txt","r")
for line in B.readlines():
    id,pw = line.split("|",1)
    print (id)
    print (pw)
    if (EnteredID == id ) and (EnteredPassword == pw):
        print ("Customer Login successful")
        B.close()
        CustomerMenu()
print ("Wrong Account Number/password")
menu()

AdminUser.txt

00001|1234

Customerlogin.txt

000403100865|3088

Output is:

000403100865

3088

Customer Login successful

Wrong Account Number/password


Solution

  • The error suggests that the problem is the following line:

    id,pw = line.split("|")
    

    If you have more than one "|" in your text your will not be able to split them this way.

    To guarantee the string is split at most once try replacing with:

    id,pw = line.split("|", 1)