Below is a program that asks the user to input a username and password. Once logged off the user is asked to re-enter their username and password.

Is the program correct where it checks if the username and password input before is correctly input again?.

Any help would be welcome

Regards

Mark

# program that checks that the username and password are correct #

username = input("Please enter your username : ")
password = input("Please enter your password : ")
print ("Greetings," , username, "you are now logged in now with a password")

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 password")

# program that checks that the username and password are correct # 

username = input("Please enter your username : ")
password = input("Please enter your password : ")
print ("Greetings," , username, "you are now logged in now with a password")

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 password")

Without code tags nobody can read your program.

Edited 4 Years Ago by pyTony: n/a

No, username and password given in first lines is never validated. Line 13 until end without beginning lines serve as good start to prepare the program.

Edited 4 Years Ago by pyTony: n/a

You should put this in a function that you can call repeatedly instead of duplicating code.

I agree with the above posts. My spin on the code would be:

def askUser():
    username = raw_input("Enter your username: ")
    password = raw_input("Enter your password: ")
    checkPass(username, password)

def checkPass(use, pwd):
    if use == "username" and pwd == "password":
        login(use)
    else:
        print "Your username and/or password was incorrect"
        askUser()

def login(use):
    print "Welcome " + use
    print "You have successfully logged in!"
    askCom()

def askCom():
    command = raw_input("Enter your command: ")
    if command == "log off" or command == "quit":
        username = ""
        password = ""
        print "You have logged off"
        askUser()
    else:
        print "Unknown command"
        askCom()

askUser()
This article has been dead for over six months. Start a new discussion instead.