3

Can someone check this code please? Most of it is working but when they type 'admin' it should allow them to set a new password 'type new password' but then the new password doent save. Can anyone help me fix it? Thanks

program = ("live")
while program == ("live"):
    password = ("Python")
    question = input("What is the password? ")
    if question == password:
        print ("well done")
    if question == ("admin"):
        n_password = input("What is the new password? ")
        password = n_password
        question = input("What is the password? ")
    else:
        question = input("What is the password? ")
4
  • What is a type of ("live")? Commented Nov 20, 2013 at 15:47
  • Just a note: please don't 'save' passwords as cleartext. When a user sets a password, you simply store an hash of that passwort. Next time he 'logs in', you check if the hashes are the same. Commented Nov 20, 2013 at 15:48
  • 1
    no need to enclose your strings in parens (you're not making a list when you do that). Commented Nov 20, 2013 at 15:53
  • Rather than using input, you might like to consider getpass, it is in the standard library and very easy to use. By the way, check your logic flow. The user will be asked for the password twice in each loop (regardless of admin or not). Commented Nov 20, 2013 at 16:13

2 Answers 2

9

You need to move the first password = ... line out of the loop:

program = ("live")
password = ("Python")
while program ==("live"):
    question=input("What is the password? ")
    if question == password:
        print ("well done")
    if question == ("admin"):
        n_password = input("What is the new password? ")
        password=n_password
        question=input("What is the password? ")
    else:
        question=input("What is the password? ")

This ensures that the password is Python the first time around but after that it'll use the new value for password. Also note that you can remove a few input() calls:

program = ("live")
password = ("Python")
while program ==("live"):
    question=input("What is the password? ")
    if question == password:
        print ("well done")
    if question == ("admin"):
        n_password = input("What is the new password? ")
        password=n_password
Sign up to request clarification or add additional context in comments.

2 Comments

Great help, much appreciated!
Please remove the unneeded parens from your answer.
2

You need to put password = ("Python") before the beginning of your while loop.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.