How can I correct this python login system?

1k Views Asked by At

I am really new to programming and I am given an exercise on creating a login system using text files. The thing is I got really confused about my codes, the login system contains two roles which are admin and customers, and customers are divided into registered and unregistered customers. Admins and registered customers are supposed to directly login into the system whereas unregistered customers are required to create a new account. Also, we are not allowed to use global variables or imports. I apologize if the codes are absolute chaos.

Here is my code:

#Role selection
role = int(input("Select your role: [Admin = 1, Customer = 2]"))

#Admin login
def adminLogin():
    if(role == 1):
        adminUsername = input("Enter your username: ")
        adminPassword = input("Enter your password: ")
        for line in open("adminLoginDetails.txt","r").readlines():
            login_info = line.split
            if (adminUsername == login_info [0] ) and (adminPassword == login_info[1]):
                print("You have successfully logged in!")
            else:
                print("Invalid username or password, please try again.")

#Customer registration
    else:
        def cusRegistration():  
            registration = input("Are you a registered customer? [Yes/No]")
            if (registration == "No"):
                cusUsername = input("Enter your username: ")
                cusPassword = input("Enter your password: ")
                file = open("customerDetails.txt","a")
                file.write(cusUsername)
                file.write(" ")
                file.write(cusPassword)
                file.write("\n")
                file.close()
                if cusRegistration():
                    print("You have successfully created an account!")
            
#Customer Login
def cusLogin():
    if (registration == "Yes"):
        cusUsername = input("Enter your username: ")
        cusPassword = input("Enter your password: ")
        for line in open("customerDetails.txt","r").readlines():
            loginInfo = line.split()
            if (cusUsername == loginInfo[0]) and (cusPassword == loginInfo[1]):
                print ("You have successfully logged in!")
            else:
                print("Invalid username or password, please try again.")
2

There are 2 best solutions below

0
SteveJ On

In your code given above, you need to pull your check for roll outside the methods that use them. Currently, nothing is asking your methods to execute.

role = int(input("Select your role: [Admin = 1, Customer = 2]"))
if role == 1:
     adminLogin()
if role == 2:
     cusLogin()
# etc
1
AudioBubble On

You need to actually run your functions:

def do_something():
    print('something')

Won't run. You need to also use this:

do_something()

In your case:

if role == 1:
    adminLogin()

etc