Python 登录程序

Python login program

我正在 Python 中编写一个登录程序,它获取用户名和密码,并为每个用户将其存储在单独的 .txt 文件中。 我已经可以注册,程序会创建文件,但我无法登录。 这是代码:

###############
import getpass
import time
ussr=False
passwd1=False
acc=False
notin=False
reg=False
t=[]
###############
#f;a;;b;c;d;z;;;;;
###############
def bor():
    global acc
    print("1-->Login")
    print("2-->Register")
    a=int(input("B/R: "))
    if  a==1:
        acc=True
    if a==2:
        acc=False

def ACCOUNT():
    if acc==True:
        login()
    if acc==False:
        register()

def file():
    global jel
    f=open(z,"r")
    sor=f.read()
    jel=sor.strip().split()
    for s in jel:
        t.append(s)
    f.close()


def register():
    global reg
    reg=True
    global z
    b=input("Username: ")
    c=getpass.getpass('Passwd')
    z=input("Filename(.txt!):")
    f2=open(z,"w")
    f2.close()
    f=open(z,"r+")
    if b not in f:
        notin=True
    f.close
    if notin==True:
        f1=open(z,"a")
        f1.write(b)
        f1.write(c)
        f1.close
    if notin==False:
        print("This username is already taken")
        exit

def login():
    global usr
    global passwd
    global passwd1
    global ussr
    global z
    usr=input("Username: ")
    passwd=getpass.getpass('Password')
    z=input("Filename(.txt!):")
    for i in t:
        if usr==i:
            ussr=True
        if passwd==i:
            passwd1=True

def check():
    if reg==True:
        exit
    if ussr==True and passwd1==True:
        print("Login succesful")
        time.sleep(12)
    if ussr==True and passwd1==False:
        print("Wrong password")
    if ussr==False and passwd1==True:
        print("Wrong username")





bor()
ACCOUNT()
file()
check()

请避免使用全局变量,多开始编程"pythonic"。开始使用 class 对象和函数参数。 真的,全局变量很糟糕。

然后:

def ACCOUNT():
    if acc==True:
        login()
    if acc==False:
        register()

这是一个可怕的说法。

def ACCOUNT(acc):
   if acc: login() 
   else: register()

好多了。

写条件的时候要考虑"if, else, elif":

if a == 1: 
   doSomething()
elif a == 2:
   doSomethingElse()
else:
   doAnotherThing()

说清楚,除非你需要检查变量类型,一般来说 "if varname" 是可以的(如果对象为空,或者布尔值等于 False,它会 return false以及 None)。

如果必须检查布尔值,通常不必指定要查找的条件。

"if not a" 比 "if a == False" 更 pythonic(但是,同样,它也会匹配 "None" 而不仅仅是 "False"!!)

并告诉自己,每次声明全局变量时:“我做错了什么,肯定有更好的方法来做到这一点

您可以将变量作为函数参数传递:

def sumFunction(arg1, arg2):
    return arg1 + arg2

并且您可以声明 classes,以存储变量、处理相同任务的函数,并更好地开始编程:

class Authentication():
    def __init__(self):
        # the __init__ class method is run everytime the function is istantiated
        self.example = 'I am an example'

    def Login(self, username, password):
        # this will do authentication things with
        # username and password variables, that lives
        # ONLY in this function (namespace)

    def Register(self, username, password):
        # do register things

然后您可以像这样实例化 class 身份验证

auth = Authentication()

并根据需要使用实例:

auth.Login(username, password)
auth.Register(username, password)

"self" 是 class 命名空间,从它本身来看。 您可以在其中存储任何您喜欢的内容,并且可以在 class 中从一个到另一个调用子函数,在 "self." 前缀

之前

在我提供的示例中,您可以通过以下方式访问"I am an example text":

>>> x = Authentication()
>>> x.example
'I am an example'

最后,当你声明一个class方法(class中的一个函数)时,你总是必须在第一个参数中指定"self",但你不需要从外部调用方法时传递它(因此具有 3 个参数的 class 方法:(self,arg1,arg2),将期望两个参数起作用,只有 arg1 和 arg2。

多读,少写,祝你好运。