Having trouble preventing users from registering with a duplicate username on Python

Viewed 21

I'm a student who is doing my Python assignment. Thing is, our lecturer is really strict on us not using python built ins and also refuses to check our code before submission and I'm having the hardest time trying to get my code to work properly during registration.

My problem currently is that I want the code to prompt an error from the program itself when the user tries to register with a taken username. However, it keeps running different errors regardless of how I try to fix it. Can anyone please point me in the right direction or tell me what's wrong with my code? I'd be really grateful for any help.

def grant():
    print("Helllooooo")

def begin():
    while True:
        print("Welcome to the Freshco Groceries App!")
        option = input("Are you a new or returning user?\n"
                       "Enter 1 if you would like to LOGIN\n"
                       "Enter 2 if you would like to register\n"
                       "Enter 3 if you would like to exit the program\n")
        if option=="1":
            access(option)
            break
        elif option=="2":
            access(option)
            break
        elif option=="3":
            print("Thank you! Have a good day.")
            exit()
        else:
            continue
        
def access(option):
    if(option=="1"):
        name = input("Please enter your username:\n")
        password = input("Please enter your password:\n")
        login(name,password)
    else:
        print("First step: Please choose a username and a unique password.")
        name = input("Please enter your username:\n")
        password = input("Please enter your password:\n")
        register(name,password)
        newuser()

def login(name,password):
    success=False
    file = open("user_details.txt","r")
    for line in file:
        a,b = line.split(",")
        if (name in a) and (password in b):
            success=True
            break
    file.close()
    if(success):
        print("You have successfully logged in! Happy shopping!")
        grant()
    else:
        print("Wrong username or password entered.")

def register(name,password):
    exist = False
    while True:
        file = open("user_details.txt", "r")
        for line in file:
            line = line.rstrip()
            if (name == line):
                exist = True
                break
            else:
                pass
        file.close()
        if (exist):
            print("Username has been taken. Please try again with a new one.")
            break
        else:
            file = open("user_details.txt","a")
            file.write("\n" + name + "," + password)
            print("You have successfully registered! Welcome to the Freshco Family!")
            grant()
            break

def newuser():
    print("Before you start using the app, please fill in the details below.")
    alldata = []
    while True:
        newdata = []
        nname = input("Please enter your full name:\n")
        newdata.append(nname)
        while True:
            ngender = input("Are you female or male?\n"
                            "1: female\n"
                            "2: male\n")
            if ngender=="1":
                    fingender="female"
                    newdata.append(fingender)
                    break
            elif ngender=="2":
                    fingender="male"
                    newdata.append(fingender)
                    break
            else:
                print("INVALID INPUT")
                continue
        naddress = input("Please enter your address:\n")
        newdata.append(naddress)
        nemail = input("Please enter your email address:\n")
        newdata.append(nemail)
        ncontact = input("Please enter your contact number:\n")
        newdata.append(ncontact)
        ndob = input("Please enter your dob in this format: <DD.MM.YY>\n")
        newdata.append(ndob)
        alldata.append(newdata)
        print(newdata)
        break
    print("Thank you for entering your information, you will be redirected now!")
    grant()

begin()


thank you so much! T-T

0 Answers
Related