如何查看我的密码变量是否在一行内,以便我的代码可以继续?

时间:2018-02-14 08:48:41

标签: python passwords user-input

我正在为学校创建一个密码重置器,我想检查一下,如果密码在文本文件的第一行内,那么它可以说"好的,选择一个新密码" )

Newpass=""
Network=""
setpass=""
Newpass=""
password=""
def newpass():
    Network=open("Userandpass.txt")
    lines=Network.readlines()
    password=input("just to confirm it is you, re-enter your old password:")
    for i in range (3):
        if password in line:
             newpass=input("Okay, choose a new password ")
             Network.close()
             Network=open("Userandpass.txt","a")
             if len(newpass)>= 8 and newpass[0].isalnum()==True and newpass[0].isupper()==True:
                print('password change successful')
                Network.write("New Password  : " + newpass )
                Network.close()
                break
             else:
                print("password did not match requirements, try again ")
        else:
            print("error")
            break
    print("3 tries up or else password updated")

Network=open("Userandpass.txt","w")
Network.write(input("What is your Username")+",")
Network.write(input("Password:")+ ",")
question=input("Do you want to change your password?")
if question=="yes":
    Network.close()
    newpass()
else:
     Network.close()
     print("Okay thank you.")

请帮忙!我一直在这里寻找,我无法找到解决方案

3 个答案:

答案 0 :(得分:0)

您可以尝试两件事:

lines=Network.read() # change here
password=input("just to confirm it is you, re-enter your old password:")
for i in range (3):
    if password == lines.split(",")[1]: # change here also

说明:

readlines的问题是o / p作为列表,其中read以字符串形式返回,这是在这里使用的更好的字母。
它作为单个字符串返回的第二件事,即名称和密码与,的组合形式。因此,如果您split,您将获得一个分隔值列表。然后,您只能从中password获取并检查输入 在您的代码中,您只是检查input元素是否存在于整个字符串中,而不是检查它是否是相同的密码

答案 1 :(得分:0)

你说你在学校需要这个,所以我认为这有点紧急:

我做的所有更改都是次要的,我评论了我做了什么以及为什么。

# no need to intialize these variables
#Newpass=""
#Network=""
#setpass=""
#Newpass=""
#password=""

def newpass():
    # the with open() construct takes care of closing the file for you and gives you a nice handle for working with the file object.
    with open("Userandpass.txt") as Network:
        # this reads all filecontents into a list and while doing so strips away any kind of whitespace.
        username,current_password=[x.strip() for x in Network.readlines()]
    for i in range (3):
        password=input("just to confirm it is you, re-enter your old password: ")
        if password == current_password:
            # communicate the requirements instead of letting potential users run into errors
            newpass=input("Okay, choose a new password (must be 8 chars or more, alphanumeric and have it's first letter capitalized.) ")
            if len(newpass)>= 8 and newpass.isalnum() and newpass[0].isupper():
                with open("Userandpass.txt","a") as Network:
                    print('password change successful')
                    Network.write("New Password  : " + newpass )
                return
            else:
                print("password did not match requirements, try again ")
        else:
            print("error")
            break
    print("3 tries up or else password updated")

with open("Userandpass.txt","w") as pwfile:
    # separate by newline instead of punctuation => personal preference
    pwfile.write(input("What is your Username? ")+"\n")
    pwfile.write(input("Password: ")+ "\n")

question=input("Do you want to change your password? (yes/no): ")

if question[0].lower() == 'y':
    newpass()
else:
    print("Okay thank you.")
    # properly terminate
    exit(0)

答案 2 :(得分:0)

你错过了

中的's'
if password in line**s**:

也许问题来自那里。