Python Forum
Python login system help. - 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: Python login system help. (/thread-8743.html)



Python login system help. - calumw20 - Mar-05-2018

Hi there! I am looking for help with a login system in python, It must contain an array and be able to save the details of users when registered. I have tried to use the array in the username part by making sure a number is in the username. I try to run this and it says my username doesn't include this number, even when it does. Im not sure where the fault lies. any help would be greatly appreaciated.

def CheckNumbers(NewUser):#This ensures that the user enters a number in their username
    number = ['0','1','2','3','4','5','6','7','8','9'] #*ARRAY* of numbers stored.
    for i in range(len(number)): #Checks for numbers in variable NewUser
        if number[i] in NewUser:
            print ("Username Accepted")#prints message
            return NewUser

        elif number[i] not in NewUser: #If number from array created is not in input, prints following msg.
            print("Username is not strong enough, include a number!")
            NewUser= input("Please enter a new username. Remember to include a number!") #Asks user to try again.


def LengthChecker(NewPassW): #This function makes sure password is long enough.
    while len(NewPassW) < 8: #Checks if the password length is less the 8
        print("Password is too short, it must be more than 7 characters!")
        NewPassW = input("Please create a new password.") # asks  user to enter password


    print("Password created.")

    return NewPassW #return new variable


def NewU(): #This Function creates new accounts for user
    NewUser= input("Create new username name")#Asks user to create a new username
    NewUser = CheckNumbers(NewUser)#calls function 'checknumbers'
    if NewUser in users: #checks if user is already in direvtory
        print("User already exists please try again.")
    else: #If user is not already in directory
        NewPassW = input("Create New Password") #Asks user to create password
        NewPassW = LengthChecker(NewPassW) #Calls Lengthchecker function with passing parameter NewPassW
        details(NewUser,NewPassW) #calls details function with passing parameter NewUser and NewPassW
        print("Your Account has been created! Welcome", NewUser)


def OldU():#This function is if the user already has an account
    user = input("Enter Your Username") #Asks user to type in their username
    passW = input("Enter Your Password") #Asks user to type in their password

    for line in open("LoginData.txt","r").readlines(): #Opens and reads the text file
        LogData = line.split()
        if user == LogData[0] and passW ==LogData[1]:
            print("Login Successful!")
        else:#if user does not exist
            print("Incorrect username or password.")



def details(NewUser,NewPassW): #This is where all the created usernames and passwords are stored
    Login = open("LoginData.txt","a+") # Creates or opens LoginData.txt file
    Login.write(NewUser)
    Login.write(" ") #Creats a space between username and password
    Login.write(NewPassW)#writes data for new password
    Login.write("\n")
    Login.close #files closes

users = {} #User Dictionary
option = "" #Define global variable "Option"

while option != "Q": #Makes sure Q was not the input.

    option = input("Welcome. Do you already have an account? Type 'Y' or 'N'.Type Q for quit ") #Prompt user to type in Y , N or Q

    if option == "Y": #Checks for Y input
        OldU() #goes to OldU Function
    elif option == "N": #Checks for N input
        NewU() # goes to NewU Function



RE: Python login system help. - buran - Mar-06-2018

the problem is that you need to check for all digits before declaring username not valid. at the moment it will print "Username is not strong enough, include a number!" and ask for new username immediately id 0 (zero) is not in the name. Also note that asking for new input within the function that checks the name is not correct - as is it will not be preserved once the function execution ends.