Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Login and Register system
#1
hi,

i have this code

def login_1(): #everything below is what will happen when we use the login() command

    print ("\n") #adds a line break
    print ("-----------------------------------------------------------------") #lines to make it more presentable
    print ("\n") #adds a line break
        
    global username_not_to_be_used #globalises the variable so it cn be used elsewhere
    global username_1 #globalises the variable so it can be used elsewehere
    username_1 = input ("Enter your username: ") #asks for a user to input there username
    username_not_to_be_used = username_1 #sets username_not_to_be_used to the same as username_1
    file_check_1 = os.path.isfile("E:/Work/School/Computer Science/Python/Code/School Work/User Info/" + username_1 + ".txt") #checks if the file exists
    if file_check_1 == False: #if the check returns as false the username dosent exist
        print ("That username dosent exist, please start again") #tells you that the username dosent exist
        start_1() #runs the start command
    if file_check_1 == True: #if the check returns as true it opens the file to check and see what the password is
        password_check_1 = open("E:/Work/School/Computer Science/Python/Code/School Work/User Info" + "/" + username_1 + ".txt","r").read() #looking at what is in the file (the password)
        password_1 = input ("Enter your password: ") #asks for the user to input there password
        

        if password_check_1 != password_1: #if the password in the file and the password the user typed arent equal
            print("You have entered an incorrect password, please start again") #it tells you the passwords dont match
            start_1() #runs the start command
	 

        elif password_check_1 == password_1: #if the password from the file and the users inputted one do match
            print ("Welcome to your account.") #says welcome to your account
            print ("\n") #adds a line break
            time.sleep(1) #waits one second till the next action

######################################################################################################

def delete_account_1(): #everything below is what will happen when we use the delete_account() command
    delete_user_1 = input ("What is the username of the account you would like to delete? ")
    delete_account_check_1 = os.path.isfile("E:/Work/School/Computer Science/Python/Code/School Work/User Info/" + delete_user_1 + ".txt") #checks if the file exists
    if delete_account_check_1 == False: #if the check returns as false the username dosent exist
        print ("That username dosent exist, please start again") #tells you that the username dosent exist
        start_1() #runs the start command
    if delete_account_check_1 == True: #if the check returns as true it opens the file to check and see what the password is
        delete_password_check_1 = open("E:/Work/School/Computer Science/Python/Code/School Work/User Info" + "/" + delete_user_1 + ".txt","r").read() #looking at what is in the file (the password)
        delete_password_1 = input ("Enter your password: ") #asks for the user to input there password
        

        if delete_password_check_1 != delete_password_1: #if the password in the file and the password the user typed arent equal
            print("You have entered an incorrect password, please start again") #it tells you the passwords dont match
            start_1() #runs the start command
	 

        elif delete_password_check_1 == delete_password_1: #if the password from the file and the users inputted one do match
            confirm_delete_1 = input ("Are you sure you want to delete this account? yes/no ") #asks if you want to delete account
            if confirm_delete_1 == "yes": #if yes is inputted
                os.remove("E:/Work/School/Computer Science/Python/Code/School Work/User Info" + "/" + delete_user_1 + ".txt") #deletes the file form user info folder
                print ("This account has now been deleted! ") #tells you the account has been deleted
                print ("-----------------------------------------------------------------") #adds lines to make it more presentable
                start_1() #runs the start_2() command
            elif confirm_delete_1 == "no": #if no is inputted
                print ("Ok we will return to the start") #says it will return to the start
                start_1() #runs start_1() command
            else: #if anyting else is inputted
                print ("That is an invalid input, please start again") #says its an invalid input
                start_1()

######################################################################################################

def start_1():#everything below is what will happen we use the start() command
    print ("Welcome to FWW login systems:") #says welcome to FWW login systems
    time.sleep(1) #adds a 1 second break
    user_option_1 = input ("Hello User 1 - Do you want to 'logon' or 'create account' or 'delete account' - please answer with one of those options ") #asks if you have an account
    if user_option_1 == "logon": #if the response is yes
        print ("\n") #adds a break
        print ("Please Login:") #says please login
        login_1() #runs login() command
    elif user_option_1 == "create account": #if the answer is no
        print ("\n") #adds a liine break
        print ("Please Register") #says please register
        print ("\n") #adds a line break
        print ("-----------------------------------------------------------------") #adds lines to make it more presentable
        print ("\n") #adds a line break
        new_account_1() #runs new_account() command
    elif user_option_1 == "delete account": #if the user inputs delete account
        delete_account_1() #runs delete account command
    else: #if anything else is inputted
        print ("That is an invalid input, please try again") #tells you its an invalid input
        start_1() #runs the start() command

######################################################################################################

def new_account_1(): #aything below is what will happen when we use new_account() command

    new_username_1 = input ("What do you want to set your username as: ") #ask user to input a new username
    username_taken_check_1 = os.path.isfile("E:/Work/School/Computer Science/Python/Code/School Work/User Info/" + new_username_1 + ".txt") #checks if the file exists
    if username_taken_check_1 == True: #is it returns as true and the file does exist
        print ("This username is already taken, please try again") #tells you the username has already been taken
        new_account_1() #runs the new_account() command
    elif username_taken_check_1 == False: #if it returns as false and the file dosent exist
        print ("Okay your new username is, " + new_username_1) #tells them what the new username is
    
    
    new_password_1 = input ("What do you want to set your password as: ") #asks what they want there password to be
    confirm_password_1 = input ("Please confirm your password: ") #asks them to retype/confirm there password

    if new_password_1 == confirm_password_1: #checks if the passwords match
        user_details_1 = open("E:/Work/School/Computer Science/Python/Code/School Work/User Info" + "/" + new_username_1 + ".txt","w") #creates a new file called "username".txt 
        user_details_1.write(new_password_1) #writes in the file what the users password is
        user_details_1.close() #saves and closes the file
        print ("Your passwords match!") #tells you they match
        print ("Your account has been created!") #tells you that your account has been created
        print ("Please now proceed to the next step") #says to proceed
        print ("\n") #adds a line break
        print ("-----------------------------------------------------------------") #lines to make it more presentable
        print ("\n") #adds a line break
        

        start_1() #runs the start_1() command

 
    else: #if anything else is inputted
        print ("You have made an error. Please start again") #tells the user they made and error
        start_1() #runs the start() command

######################################################################################################

start_1() #runs the start() command
basically its a login system.

after you have logged in it should take you to the main part of the program which it does most of the time. however when i do the following i get an output i dont wnat:

create account
use a taken username
try a different non taken username set a password
confirm it wrong
choose logon
login correctly


after i do those steps its says
welcome to your account

what do you want to set your passsword as:

after welcome to your account i want it to move on.

ill show you the output

Output:
Welcome to FWW login systems: Hello User 1 - Do you want to 'logon' or 'create account' or 'delete account' - please answer with one of those options create account Please Register ----------------------------------------------------------------- What do you want to set your username as: test This username is already taken, please try again What do you want to set your username as: tetset Okay your new username is, tetset What do you want to set your password as: t Please confirm your password: g You have made an error. Please start again Welcome to FWW login systems: Hello User 1 - Do you want to 'logon' or 'create account' or 'delete account' - please answer with one of those options logon Please Login: ----------------------------------------------------------------- Enter your username: test Enter your password: test Welcome to your account. What do you want to set your password as:
when it asked for your passwrod that shouldnt happen,

whatever happens before i want it so that when you finally login successfully i want it to move on to what is next in the program. which is just it asking player 2 to login.

thanks
"Only Boring People Get Bored"
Reply
#2
You call new_account_1() recursively if the user types in a username that is already in use.

def new_account_1(): #aything below is what will happen when we use new_account() command
 
    new_username_1 = input ("What do you want to set your username as: ") #ask user to input a new username
    username_taken_check_1 = os.path.isfile("E:/Work/School/Computer Science/Python/Code/School Work/User Info/" + new_username_1 + ".txt") #checks if the file exists
    if username_taken_check_1 == True: #is it returns as true and the file does exist
        print ("This username is already taken, please try again") #tells you the username has already been taken
        new_account_1() # <-- Recursively call again
    elif username_taken_check_1 == False: #if it returns as false and the file dosent exist
        print ("Okay your new username is, " + new_username_1) #tells them what the new username is
You are seeing the password asked for twice because you called new_account_1() twice, once from inside start1() and once from inside new_account_1(). If the user entered two invalid usernames the program would ask for a password three times, one each for the invalid usernames and one for the valid username. If the program actually created a user account, it would create accounts for the invalid usernames. What a mess!

I think you will face similar problems with recursively calling start_1()

Recursion is not a good tool for this problem. Looping is a better fit.
username_taken = True
while username_taken:
    new_username_1 = input ("What do you want to set your username as: ") #ask user to input a new username
    username_taken = os.path.isfile(
        "E:/Work/School/Computer Science/Python/Code/School Work/User Info/" \
        + new_username_1 + ".txt") #checks if the file exists
    if username_taken:
        print ("This username is already taken, please try again") #tells you the username has already been taken
This will loop until an unused username is entered. No mater how many times the user enters an invalid password it still only creates one account and only asks for one password
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pymodbus Write value to register stsxbel 10 7,887 Aug-18-2022, 01:42 PM
Last Post: DeaD_EyE
  Simple syntax to asynchronously get access to MODBUS register orion67 1 2,787 Jan-22-2022, 12:40 PM
Last Post: orion67
  in a login interface when i try login with a user supposed to say test123 but nothing NullAdmin 3 2,218 Feb-20-2021, 04:43 AM
Last Post: bowlofred
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,493 Jan-11-2021, 06:30 PM
Last Post: ykumar34
  AttributeError: 'Register' object has no attribute 'bit_7' colt 1 1,959 Dec-12-2019, 09:39 PM
Last Post: micseydel
Question Difference between Python's os.system and Perl's system command Agile741 13 6,655 Dec-02-2019, 04:41 PM
Last Post: Agile741
  Login system not working Unknown_Relic 2 2,227 Nov-05-2019, 12:07 PM
Last Post: buran
  How to implement class register? AlekseyPython 0 1,961 Feb-14-2019, 06:19 AM
Last Post: AlekseyPython
  Login System Carbonix 4 4,382 Feb-04-2019, 02:19 PM
Last Post: Larz60+
  simple register code aocii 2 2,590 Dec-22-2018, 11:15 AM
Last Post: aocii

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020