Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? ")
like image 712
thankful_1 Avatar asked Jul 14 '26 05:07

thankful_1


2 Answers

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
like image 176
Simeon Visser Avatar answered Jul 15 '26 19:07

Simeon Visser


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

like image 32
quazzieclodo Avatar answered Jul 15 '26 19:07

quazzieclodo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!