我收到此代码的IO错误但它有效。我不明白

时间:2017-04-19 14:31:54

标签: python

我刚刚为我的Python课程做了额外的工作,通过在这里和那里创建小程序来提高我的技能。我编写了一个def,要求输入用户名,检查文件,并将其写入文件,如果没有与给定的用户名匹配的当前用户名。然后我写了一个新的def然后请求PW,再次检查pw,并在给定的用户名后写。唯一的问题是我收到错误,但它仍然有效并在名称后面写了pw。知道如何解决这个问题吗?

Traceback (most recent call last)
File "C:blah blah dir to .py", line 72, in getnewpassword
    for line in settingpassword
ValueError: I/O operation on closed file

PW的def代码是

def getnewpassword(tempname):
    passwordPassed = False
    passwordComplete = False
    newpassword = raw_input('Please enter a password for ' + tempname + '.' + ' Please note that you cannot use spaces when creating a password.\n')
#makes sure there are no spaces in password.
    if ' ' in newpassword:
        print 'Sorry but your password has a space in it. Remember, you cannot have spaces in your password. Try again.\n'
        getnewpassword(tempname)
        passwordPassed = False
    else:
        print "Password accepted.\n"
        passwordPassed = True
#asks for password input again and checks to see if both password variables match.
    if passwordPassed == True:
        retypedpassword = raw_input('Please enter your password again.\n')
        if newpassword == retypedpassword:
            print "User account created."
            setuserpassword = retypedpassword
            passwordComplete = True
        else:
            print "passwords don't match. Please try again"
            getnewpassword(tempname)
#attempts to write password after username and :. works but gives an IO error.
    if passwordComplete == True:
        settingpassword = open('accounts.txt','r+')
        for line in settingpassword:
            if tempname in line:
                settingpassword.write(setuserpassword + '\n')
                settingpassword.close()

0 个答案:

没有答案