简体   繁体   中英

Python Login Page

Hi I am trying to make a login page for a movie streaming website can anyone help me improve this code please

I need it to save the password and username and when it has done that i need that username to be invalid to anyone who trys to use it again to make an account

username = input("Please enter your username : ")
password = input("Please enter your password : ")
print ("Greetings," , username, "Please re enter your password to confirm 
it")
command = input("Please type a command :")
if command == "log off":
print("You have now been logged off again",username)
username == ""
password == ""

username = input("Please enter your username : ")
password = input("Please enter your password : ")

while username != "username" and password != "password":

    print (" Sorry username and password incorrect please re-enter for 
validation ")
    username = input("Please enter your username : ")
    password = input("Please enter your password : ")

else:
print ("Greetings," , username, "you are now logged in now with your new 
password")

username = input("Please enter your username : ")
password = input("Please enter your password : ")
print ("Greetings," , username, "Please re enter your password to confirm 
it")
command = input("Please type a command :")
if command == "log off":
print("You have now been logged off again",username)
username == ""
password == ""

file = open("testfile.txt","w") 

file.write("Usernames and Passwords") 
file.write(("Username :") username + ("Password :") password) 


file.close() 

I kind of made an elaborate program, I hope you can appreciate the time I spend on it. I think this questions was better asked on codereview since it does not ask about a specific bug but rather how to improve your code in general.

First of all I made some functions for reusability. That way the code becomes more readable and makes more sense in a whole. Secondly I added new_user() functionality. May I remind you SO is not a codewriting service, you should count yourself lucky I had nothing better to do

def log_in():
    username = input("Please enter your username : ")
    password = input("Please enter your password : ")

    with open('test_file.txt', 'r') as file:
        for line in file:
            if line == 'Username:{0}, Password:{1}'.format(username, password):
                print ("Greetings," , username, "you are now logged in")
                return True, username, password
    print (" Sorry username and password incorrect please re-enter for validation ")
    return False, '', ''

def new_user():
    succes = False
    while not succes:
        new_user = input("Please enter your new username : ")
        new_pass = input("Please enter your new password : ")

        exists = False
        with open("test_file.txt","r") as file:
            for line in file:
                if line.split(',')[0] == 'Username:'+new_user:
                    print ('Invalid username: {0} already exsist'.format(new_user))
                    exists = True

        if not exists:
            with open("test_file.txt","a") as file:
                file.write('Username:{0}, Password:{1}'.format(new_user, new_pass))
            succes = True
    print ('You made a new user with username:{0} and password:{1}'.format(new_user, new_pass))

def main():
    command = username = password = ''
    logged_in = False
    while command != 'quit':
        command = input('Please type a command: ')
        if command == 'log in':
            logged_in, username, passowrd = log_in()
        if command == 'log out':
            logged_in = False
            username = passowrd = ''
        if command == 'new user':
            if not logged_in:
                new_user()
            else:
                print ('First logout to make a new user')

main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM