为了账号安全,请及时绑定邮箱和手机立即绑定

我做错了吗?

我做错了吗?

料青山看我应如是 2022-08-25 14:50:39
我刚刚开始学习Python编程,我正在尝试创建一个登录系统,该系统使用终端并将数据存储到JSON文件中并检索它以登录。这是导致问题的部件。def login():    login_username = input("Enter your username: ")    login_password = input("Enter your pass: ")    with open("data.json", 'r+') as data:        if login_username == username and login_password == password:            print("Successful Login")        else:            print("Please Try Again")            login()当我被要求输入用户和传递并且输入与JSON中的数据匹配时,它将循环并要求一次又一次地输入用户名和密码。我的整个代码在下面import jsonuname = ""password =""def register():    uname = input("Enter a user to log in with: ")    password = input("Enter a password: ")    confirmed_pass = input("Enter the above password again")    if password != confirmed_pass:        print("Both the passwords does not match please re-enter a pass")        password = input("Enter a password:")        confirmed_pass = input("Enter the above password again")    login_info = {        "Username": uname,        "Password": password    }    with open("data.json", "w") as write_file:        json.dump(login_info, write_file, separators=(',', ':'))    log = input("Would you like to login? (Y/N)")    if log == 'Y' or log == "y":        login()    else:        quit()def login():    login_uname = input("Enter your username: ")    login_password = input("Enter your pass: ")    with open("data.json", 'r') as data:        if login_uname == uname and login_password == password:            print("Successful Login")        else:            print("Please Try Again")            login()reg = input("Have you registered (Y/N) ?:  ")if reg == 'Y' or reg == 'y':    login()elif reg == 'N' or reg == 'n':    register()else:    print("Error!")
查看完整描述

1 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

根据您的代码,您应该首先使用json.load将数据加载到python字典中:

with open("data.json", 'r') as data:
    login = json.loads(data.read())    # then you can use it like this
    if login_username == login['Username'] and login_password == login['Password']:
        ... Rest of the code

只是一个头,您的登录名仅适用于最后一个注册用户,您应该解决这个问题。


查看完整回答
反对 回复 2022-08-25
  • 1 回答
  • 0 关注
  • 91 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号