【发布时间】:2019-06-18 15:12:26
【问题描述】:
这是一个恢复系统,用户可以在其中输入他们的唯一代码来恢复他们的详细信息。当输入唯一代码时,它会从文本文件中获取他们的详细信息,然后打印他们的用户名和密码。然后他们可以选择更改密码。我遇到的问题是,当我在其中输入旧密码时,它说密码不在该特定行中。文件中的格式是这样的:
(unique code) username: (username) password: (password)
global password1, password2,
check = True
while check:
hash1=input("\n"+"Enter your unique code to recover your username or password ")
with open("mount.txt","r") as file:
for line in file:
text = line.strip().split()
if hash1 in text:
print(line)
check = False
change = input("Do you want to change your password? [y,n]")
while change not in('y', 'n'):
change = input("Do you want to change your password? [y,n]")
if change == "y":
check = True
with open("mount.txt","r+") as file:
for line in file:
text = line.strip().split()
while check:
password1 = input("Enter old password ")
password2 = input("Enter new password ")
if len(password2) < 5:
print("New password is too short")
elif password1 not in text:
print("Old password is incorrect")
elif password1 in text:
s = open("mount.txt").read()
s = s.replace(password1, password2)
f = open("mount.txt", 'w')
f.write(s)
f.close()
print("Password has been successfully changed")
check = False
elif len(hash1) < 32 and (check == True):
print("Unique code not found please try again")
break
因此,当我输入唯一代码时,它会显示正确的行,然后当我尝试更改密码时,它会一直说密码不在该特定行中。
【问题讨论】:
-
不需要在开始时将这两个变量声明为全局变量。它们在函数之外。在函数之外的 Python 中使用 this 关键字没有任何意义,也没有任何作用。
-
@Torxed 哦好的,我会从我的代码中删除它
-
另外,
if password1 in text意味着如果密码是Abcdefghijlk,我所要做的就是输入b,这将等于True。我猜这很糟糕。 -
@Torxed 这有点奇怪,因为我在文件
Abcdefghijlk中输入了密码,然后输入b仍然显示旧密码不正确 -
@Torxed 你也知道如何解决这个问题吗?
标签: python python-3.x recovery