登录帐户或注册

时间:2017-07-31 21:31:28

标签: python python-3.x

我正在尝试制作一个程序,允许用户在第一次访问时输入用户名和密码,并将其保存到外部文件中。这是它的工作,但我希望程序直接跳过登录,如果在外部文件中找到文本,因为这意味着他们已经有一个帐户。然后用户登录并且程序将其与外部文件中的数据进行比较,并且只会继续,直到输入正确的用户名和密码。

我尝试输入代码但出现了这些错误:

Traceback (most recent call last):
line 27, in <module>
    login()
line 16, in login
    check()
line 19, in check
    if username == open("username").read() and passsword == open("password").read():
NameError: name 'username' is not defined
def make_account():

    filename = ("username");
    with open (filename, "w") as f:
      f.write (input("Enter a username: "));

    filename = ("password");
    with open (filename, "w") as f:
      f.write (input("Enter a password: "));


def login():
    username = input("Enter your username: ")
    password = input("Enter your password: ")
    check()

def check():
    if username == open("username").read() and passsword == open("password").read():
        print("Successful login")
    else:
        print('Incorrect')


import os.path
if os.path.exists("username"):
    login()
else:
    make_account()

1 个答案:

答案 0 :(得分:2)

usernamepassword不在check函数的范围内。你需要传递它们:

def login():
    username = input("Enter your username: ")
    password = input("Enter your password: ")
    check(username, password) # note: passing in here

def check(username, password): # accept the parameters here
    if username == open("username").read() and password == open("password").read():
        print("Successful login")
    else:
        print('Incorrect')