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

2024-05-23 14:23:15 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我有我写的这个代码,我想要它做的就是得到用户名,密码,密码确认; 用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()

Tags: 密码inputrawobjectpasswordhashwordhashlib
1条回答
网友
1楼 · 发布于 2024-05-23 14:23:15

你不能: 而不是

 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

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

相关问题 更多 >