2013-11-20 76 views
3

有人可以檢查此代碼嗎?它們大部分都在工作,但是當他們輸入'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? ") 
+0

什麼是(「live」)類型? – itdxer

+0

請注意:請不要將密碼保存爲明文。當用戶設置密碼時,只需存儲該密碼的散列。下次他'登錄'時,你檢查哈希值是否相同。 – FeinesFabi

+0

@itdxer'basestring' – Fabian

回答

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 
+0

偉大的幫助,非常感謝! –

+0

請從您的答案中刪除不需要的parens。 – Matthias

2

你需要你的while循環開始前放password = ("Python")