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

Recommended Answers

All 9 Replies

# 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.

commented: #USERNAME AND PASSWORD username = input("Please enter your username : ") password = input("Please enter your password : ") if (username != "todd" or +0
commented: the while loop needs a colon on line 16 or you will get an error +0

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.

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()

Your all happy

Thanks 4 the support on my coding, Helps me with my GCSE's, Helped me understand !=, Cheers.

@WolfSheild how to add a username in your program????

but what if i have multiple users or you can say that i want yo create by dictionary

Typically you would create a dictionary where the username is the key and the password is the value (hopefully, encrypted).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.