Python Forum
Password and Username Verification
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Password and Username Verification
#1
Hello, I have been doing my coursework for computer science and my teacher advised that we go home and practice python in between hours in the coursework, I need help on Password and Username Verification as it requires the password and username to be stored in a .txt file outside of the program. I do not know how to check the .txt file against the logon information. This also must be able to work with more than one username and password. Thanks

I don't know if this violates the homework terms, if so please let me know.

Name = input("What is your first Name?\n")
while True:
    try:
        Age = int(input("What is your age?\n"))
        Year = int(input("What is your year group?\n"))
        break
    except ValueError:
        print ("Error, Try again")
NameFirst = Name[0]
NameMain = Name[1:3]
Name = NameFirst.upper()+NameMain.lower()
#print (Name)
Username = (Name + str(Age))
#print (Username)
print ("This is your username for the quiz",Username)
while True:
    Password = input("Please Enter a Password\n")
    PasswordCheck = input("Please Re Enter the password\n")
    if Password.lower() == PasswordCheck.lower():
        break
    else:
        print ("Passwords Do Not Match!\nPlease Re Try.")
f = open( 'Users.TXT', 'a' )
f.write(Username + '\n' + Password + '\n')
f.close()

#Logon
file = open('Users.TXT', 'r')
Data = file.read()
file.close()
#print (Data)
while true
LogonUsername = input("Please Enter Your Username\n")
Reply
#2
>>> I do not know how to check the .txt file against the logon information.

users.txt:
dan
alwaysnew
sting
beat

#Logon
user  = "alwaysnew"

found = False
with open('Users.TXT') as file:
    for line in file:
        line = line.strip()
        if user == line:
            print("user found")
            found = True
            break

if not found:
   print("user not found")
Reply
#3
That's a good start, points me in the right direction. Thanks

I have made this with the code you made but the first entry in the txt files its the only thing it reads. any answers?

def yes():
    user = input('Please enter your username\n')
    found = False
    with open('Users.TXT') as file:
        for line in file:
            line = line.strip()
            if user == line:
                print("User Found")
                found = True
                passcheck = input('Please enter you password\n')
                pfound = False
                with open('Passwords.TXT') as pfile:
                    for line in pfile:
                        line = line.strip()
                        if passcheck == line:
                            print("Password Found")
                            pfound = True
                            menu()
                            break
                        if not pfound:
                            print ('Password not found')
                            yes()
            if not found:
                print("user not found")
                yes()

def no():
    Name = input("What is your first Name?\n")
    while True:
        try:
            Age = int(input("What is your age?\n"))
            Year = int(input("What is your year group?\n"))
            break
        except ValueError:
            print ("Error, Try again")
    NameFirst = Name[0]
    NameMain = Name[1:3]
    Name = NameFirst.upper()+NameMain.lower()
    #print (Name)
    Username = (Name + str(Age))
    #print (Username)
    print ("This is your username for the quiz",Username)
    while True:
        Password = input("Please Enter a Password\n")
        PasswordCheck = input("Please Re Enter the password\n")
        if Password.lower() == PasswordCheck.lower():
            break
        else:
            print ("Passwords Do Not Match!\nPlease Re Try.")
    f = open( 'Users.TXT', 'a' )
    f.write(Username + '\n')
    f.close()
    f = open( 'Passwords.TXT', 'a' )
    f.write(Password + '\n')
    f.close()
    yes()
    
                    
        
def menu():
    File = open ('Menu.TXT', 'r')
    Menu = File.read()
    print(Menu)
    global MenuChoice
    MenuChoice = input('Please choice 1-4\n')

#menu()
##print(MenuChoice)
#Main Functions
#Password and username generator

checkuser = input("Do you have a username?\nY for Yes\nN for No\n")
if checkuser.upper() == 'Y':
    yes()
if checkuser.upper() == 'N':
    no()
Reply
#4
An extra password file does not make sense.
You have to put the password in the user.txt file:
    dan:password
    alwaysnew:secret
    sting:password
    beat:password3

#Logon
user   = "alwaysnew"
passwd = "secret"
 
found = False
with open('Users.TXT') as file:
    for line in file:
        uu, pp = line.strip().split(':')
        if user == uu and passwd == pp:
            print("user found")
            found = True
            break
 
if not found:
   print("user not found")
Reply
#5
You can store the login information in username,password form ( comma separated values ). Not in a plain text of course. Password must be hashed, the username if you want.
So, get the name and the password then open the file, check for the name match in each row and compare the hashes.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  function-decorator , which is checking an access according to USERNAME Liki 6 613 Feb-17-2024, 03:36 AM
Last Post: deanhystad
  python code tp determin a userName and print message jackAmin 4 1,867 Nov-20-2022, 12:03 AM
Last Post: rob101
  Help! Chatting App - Changing the "IP & Port" into Username Rowan99 0 1,425 Dec-20-2021, 07:02 AM
Last Post: Rowan99
  Username and password Steve2017 13 9,942 Sep-03-2017, 09:17 PM
Last Post: Steve2017

Forum Jump:

User Panel Messages

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