Login and Register system - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Login and Register system (/thread-26231.html) |
Login and Register system - finndude - Apr-24-2020 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() commandbasically 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 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 RE: Login and Register system - deanhystad - Apr-24-2020 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 isYou 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 takenThis 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 |