thankful_1
thankful_1

Reputation: 49

How to change a variable within a program

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

Upvotes: 3

Views: 97

Answers (2)

quazzieclodo
quazzieclodo

Reputation: 831

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

Upvotes: 2

Simeon Visser
Simeon Visser

Reputation: 122336

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

Upvotes: 9

Related Questions