简体   繁体   English

用户名和密码未保存到文本文件

[英]Username and Password not saving to text file

So I am writing a program that does multiple tasks but I am trying to add a section where a new user can sign up with a login.所以我正在编写一个执行多项任务的程序,但我正在尝试添加一个新用户可以通过登录名注册的部分。 I created the code that opens up a text file that stores the username and password, checks to see if the username is already in use, and then adds the new username and password that can then be used in a authentication program to verify that the username and password belong to each other and this all works great on its own.我创建的代码打开了一个存储用户名和密码的文本文件,检查用户名是否已被使用,然后添加新的用户名和密码,然后可以在身份验证程序中使用以验证用户名和密码属于彼此,这一切都很好。 The problem I run into is when I add it to my main program it will run through the code and then after saying "Account Created" it jumps back to the main menu for the user to do more things but when I check the file to see if the username and password have been added there is no new username or password.我遇到的问题是,当我将它添加到我的主程序时,它会运行代码,然后在说“帐户已创建”之后,它会跳回主菜单让用户做更多事情,但是当我检查文件以查看如果已添加用户名和密码,则没有新的用户名或密码。 Why is it not storing the information?为什么不存储信息? For context all functions in code are defined within the same python file.对于上下文,代码中的所有函数都在同一个 python 文件中定义。

def signup():
    clear()
    print("Create an account!")
    userLoginInfo = open("usersInfo.txt", 'r')
    Username = input("Create Username:")
    Password = input("create Password:")
    usernamelist = []
    passwordlist = []
    for i in userLoginInfo:
        usernameEntered, passwordEntered = i.split(", ")
        passwordEntered = passwordEntered.strip()
        usernamelist.append(usernameEntered)
        passwordlist.append(passwordEntered)
    data = dict(zip(usernamelist, passwordlist))

    if Username in usernamelist:
        print("Username Already Exists, Please Try again")
        signup()
    else:
        userLoginInfo = open("usersInfo.txt", "a")
        userLoginInfo.write("\n"+Username+", "+Password)
        print("Account Created")
    main_menu()

Your code is really great all you're missing is...您的代码真的很棒,您所缺少的只是...

if Username in usernamelist:
    print("Username Already Exists, Please Try again")
    signup()
else:
    userLoginInfo = open("usersInfo.txt", "a")
    userLoginInfo.write("\n"+Username+", "+Password)
    userLoginInfo.close()
    print("Account Created")
main_menu()

You need to close the txt file after writing to it.您需要在写入后关闭 txt 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM