登录帐户错误

时间:2014-09-07 18:28:11

标签: python

我正在尝试制作基于文本的帐户系统。基本上,用户创建帐户并将信息保存到名为帐户的字典中。每个帐户分为两部分,即用户名和密码。将信息保存到帐户字典时,会将其另存为帐户[newusername] = newpassword。无论如何我所做的登录功能都不起作用,我输入的任何内容都不起作用,只是说我输入的内容是错误的。

这是我的登录代码:

def login():
    username = ""
    password = ""
    print "\n--------------------------------------------"
    print "\n Would you like to login?"
    yesorno = raw_input("\n Please enter YES or NO: ")
    if yesorno.lower() == "no":
        print "\n--------------------------------------------"
        startup()
    elif yesorno.lower() == "yes":
        while username == "":
            username = raw_input("\n Please enter your username: ")
        while password == "":
            password = raw_input("\n Please enter your password: ")
        for usernames in accounts:
            if username.lower() == usernames.lower():
                for passwords in accounts.values():
                    if password == passwords:
                        print "\n Access Granted"
                        print "\n--------------------------------------------"
                        menu()
                    else:
                        pass
                    print "\n Access Denied"
                    print "\n Please try again"
                    login()
            else:
                pass
            print "\n Access Denied"
            print "\n Please try again"
            login()
    else:
        login()

然后这是我对整个事情的完整代码:

import datetime
import time
import pickle

filesfile = "filesfiles"
accountfile = "accountsfiles"

accounts = {}
files = {}

accountsaver = open(accountfile,'r')
accounts = pickle.load(accountsaver)
accountsaver.close()

filesaver = open(filesfile,'r')
files = pickle.load(filesaver)
filesaver.close()

def startup():
    accountsaver = open(accountfile,'wb')
    pickle.dump(accounts, accountsaver)   
    accountsaver.close()
    print "\n          -------------------          "
    print "          FILE SYSTEM MANAGER          "
    print "          -------------------          "
    print "\n To login type in: LOGIN"
    print " To create a new account type in: ACCOUNT"
    loginornew = raw_input("\n Please enter LOGIN or ACCOUNT: ")
    if loginornew.lower() == "login":
        login()
    elif loginornew.lower() == "account":
        newaccount()
    else:
        startup()

def newaccount():
    newusername = ""
    newpassword = ""
    newpasswordagain = ""
    print "\n--------------------------------------------"
    print "\n Would you like to create a new account?"
    yesorno = raw_input("\n Please enter YES or NO: ")
    if yesorno.lower() == "no":
        print "\n--------------------------------------------"
        startup()
    elif yesorno.lower() == "yes":
        while newusername == "":
            newusername = raw_input("\n Please enter a username for your account: ")
        while newpassword == "":    
            newpassword = raw_input(" Please enter a password for your account: ")
        while newpasswordagain == "":
            newpasswordagain = raw_input(" Please confirm the password for your account: ")
        if newpassword == newpasswordagain:
            for username in accounts:
                if username.lower() == newusername.lower():
                    print "\n Username already exists"
                    print " Please try again"
                    newaccount()
                else:
                    pass
            accounts[newusername] = newpassword
            print "\n Account Created"
            print "\n--------------------------------------------"
            startup()
        else:
            print "\n Passwords do not match"
            print " Please try again"
            newaccount()
    else:
        newaccount()

def login():
    username = ""
    password = ""
    print "\n--------------------------------------------"
    print "\n Would you like to login?"
    yesorno = raw_input("\n Please enter YES or NO: ")
    if yesorno.lower() == "no":
        print "\n--------------------------------------------"
        startup()
    elif yesorno.lower() == "yes":
        while username == "":
            username = raw_input("\n Please enter your username: ")
        while password == "":
            password = raw_input("\n Please enter your password: ")
        for usernames in accounts:
            if username.lower() == usernames.lower():
                for passwords in accounts.values():
                    if password == passwords:
                        print "\n Access Granted"
                        print "\n--------------------------------------------"
                        menu()
                    else:
                        pass
                    print "\n Access Denied"
                    print "\n Please try again"
                    login()
            else:
                pass
            print "\n Access Denied"
            print "\n Please try again"
            login()
    else:
        login()

1 个答案:

答案 0 :(得分:0)

在python中,每个函数都有自己的 nametable ,这基本上意味着任何变量的任何更改都会在函数完成后丢失。如果要更改所谓的全局变量(全局命名空间中的变量,函数外部的变量)而不是局部变量(命名空间中的变量)例如,一个函数),首先必须将其声明为全局:

def foo():
    global accounts
    accounts = "whatever"

这是主要问题。但您的代码仍然无法正常工作:
你这样做的方式,用户名和密码不一定合适,只需要在你的账号中,在任何地方:

for usernames in accounts: #Iteration over all usernames
            if username.lower() == usernames.lower():
                for passwords in accounts.values(): #Iteration over all passwords
                    if password == passwords:
                        print "\n Access Granted"
                        print "\n--------------------------------------------"
                        menu()

你最好像这样解决它:

for account in accounts.items():
    if account[0] == username and account[1] == password:
        print "\n Access Granted!!!"

还有另一个问题:(function:newaccount())

for username in accounts:
                if username.lower() == newusername.lower():
                    print "\n Username already exists"
                    print " Please try again"
                    newaccount() #[1]
                else:
                    pass
                accounts[newusername] = newpassword

您想要做什么:重启功能。但是在这里,您正在重新启动内部函数已经运行的函数。在#[1]成功完成后,您的程序将继续在#[1]之后的行继续,并更改已存在的用户的密码。 (它不应该,不是吗?)

要解决这个问题,只需将正确的代码放在else部分中,如果函数的另一个实例是doin'那么就打破for循环。工作:

for username in accounts:
                if username.lower() == newusername.lower():
                    print "\n Username already exists"
                    print " Please try again"
                    newaccount()
                    break #!!!
                else: #!!
                    accounts[newusername] = newpassword
                    print "\n Account Created"
                    print "\n--------------------------------------------"
                    startup()

...希望它清楚我的意思......