如何更改程序中的变量

时间:2013-11-20 15:41:25

标签: python variables for-loop

有人可以查看此代码吗?其中大部分都在工作,但当他们输入'admin'时,应该允许他们设置一个新密码'输入新密码',然后新密码保存。任何人都可以帮我修复它吗?感谢

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

2 个答案:

答案 0 :(得分:9)

您需要将第一条password = ...行移出循环:

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

这确保密码首次出现Python,但在此之后它将使用password的新值。另请注意,您可以删除一些input()来电:

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

答案 1 :(得分:2)

您需要在while循环开始之前放置password = ("Python")