python - 我如何让这个 python 登录/注册程序工作? (我是Python新手)

标签 python python-3.x authentication

所以我对 python 很陌生(在尝试学习 python 之前了解 C#),我想制作一个小登录/注册程序,我写了这一切,理论上(至少对我来说)它会起作用,但事实并非如此,它不会独立记录该帐户是否存在......

如何让这个程序发挥作用?

command = ""
username = ""
password = ""

def Register():
    global username
    global password
    username = input("Username: ")
    password = input("Password: ")

    f = open("userinfo.txt", "a")
    f.write("--------------------")
    f.write("\n%s information:\n"%(username))
    f.write("%s \n"%(username))
    f.write("%s \n"%(password))
    f.write("--------------------")

def Login():
    global username
    global password
    f = open("userinfo.txt", "r")
    f.readline()
    f.readline()
    username = f.readline()
    password = f.readline()
    f.readline()
    return username
    return password

def CheckLogin():
    global username
    global password
    global loginusername
    global loginpassword

    if loginusername == username and loginpassword == password:
        print("You´ve logged in, wait for next updates")

    elif loginusername == username and loginpassword != password:
        print("Wrong password")

    elif loginusername != username and loginpassword == password:
        print("Wrong username")

    elif loginusername != username and loginpassword != password:
        print("Wrong login information, restart the program and register to create an account")

    else:
        print("ERROR: Uknown error")


command = input("Would you like to login or register?\n")
if (command == "register"):
    Register()
    print("Done!")

elif (command == "login"):
    print("--------LOGIN--------")
    loginusername = input("Username: ")
    loginpassword = input("Password: ")
    Login()
    CheckLogin()



else:
    print("Invalid command")
    print("Please restart the program and try again")

那么,我需要制作与类(class)相关的东西吗? 比如,用户是对象? 但我如何保存这些数据?

请帮助我

编辑:对代码做了一些小的修改,修复了一个错误,但现在我有其他...

最佳答案

这是你的问题:

if (loginusername == username , loginpassword == password):

这将创建一个包含两个 bool 值的元组,并询问该元组是否为真。既然它不是空的,那就是。因此这个 if 语句的主体总是执行。

你想要:

if loginusername == username and loginpassword == password:

您在其他地方犯了同样的错误。

注意您不需要将 if 语句中的条件括在括号内。

关于python - 我如何让这个 python 登录/注册程序工作? (我是Python新手),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48450648/

相关文章:

python - YouTube发送实时聊天消息不起作用

python - 为什么 python 不将异常记录到 syslog(但它确实记录了?)

python - 与空 numpy 数组连接

python-3.x - 如何在 Python 单元测试中模拟 readlines()

python - 将整数数组转换为 unicode

PHP LDAP 使用哈希绑定(bind)

python - 如何计算Python中一组内的日期时间之间的差异?

python - 在Python中读取PostgreSQL数组数据

CakePHP $this->Auth->login() 不起作用

facebook - 如何将 Facebook SDK 与 Nuxt.js 一起使用?