在我的代码中插入while循环的位置?

时间:2015-09-23 15:29:32

标签: python while-loop md5

所以,我有这个代码,我写的所有我希望它做的是获取用户名,密码,密码确认; 使用MD5对其进行加密并将其保存到文件中。

现在我不知道在哪里插入循环,以便在密码不匹配时告诉用户。

这是我到目前为止所做的:

import os
import hashlib

def Main():

foods = ["username:", " \nPassword:", " \nConfpassword:"]

newU=str(raw_input("Username: "))

Password=str(raw_input("Password: "))
hash_object = hashlib.md5(Password.encode())
Password=hash_object.hexdigest()


Confpassword=str(raw_input("Password Again: "))
hash_object2 = hashlib.md5(Confpassword.encode())
Confpassword=hash_object2.hexdigest()

    if Password!=Confpassword:
    print("not match try again")
    newp=str(raw_input("again: "))
    newp=hashlib.md5(newp.encode())
    if newp.hexdigest()==hash_object.hexdigest():
        print("match")
        pass
    foods.insert(1,newU)
    foods.insert(3,hash_object.hexdigest())
    foods.insert(5,newp.hexdigest())
    with open("text.t" ,"w") as f:
            for word in foods:
                f.write(word)


else:
    pass

    #foods[0]=newf
    foods.insert(1,newU)
    foods.insert(3,hash_object.hexdigest())
    foods.insert(5,hash_object2.hexdigest())
    with open("text.t" ,"w") as f:
            for word in foods:
                f.write(word)


   if __name__ == '__main__':
           Main()

1 个答案:

答案 0 :(得分:0)

你不能这样做: 而不是

 if Password!=Confpassword:

这样做..

while Password!=Confpassword:
    Password=str(raw_input("Password: "))
    hash_object = hashlib.md5(Password.encode())
    Password=hash_object.hexdigest()
    Confpassword=str(raw_input("Password Again: "))
    hash_object2 = hashlib.md5(Confpassword.encode())
    Confpassword=hash_object2.hexdigest()
    #Get user to input password again

我知道它不是一个完整的解决方案,但它绝对应该告诉你你要去哪里?如果没有,请告诉我,我会把它充实