如何成功地“附加”文本文件并添加到其中?

2024-06-28 20:31:28 发布

您现在位置:Python中文网/ 问答频道 /正文

def runlogin():
    if choice1 == "yes":    
        username=input("Enter your username")
        password=input("Enter your password")

        file = open("logins.txt","r")
        if (file.readline()) == (username+":"+password):
            print("Access Granted")
        else:
            print("Access Denied")


print("Welcome")
choice1=input("Have you already created an account? ('yes' or 'no')")
choice1=choice1.lower()

if choice1 == "no":
    createusername=input("Please enter a username")
    createpassword=input("Please enter a password")
    filefile=open("logins.txt","a")
    filefile.write(createusername+":"+createpassword)
    print("Account created, redirecting to login page")
    filefile.close()


if choice1 == "yes":    
    runlogin()

当用户选择“否”时,系统会提示他们创建帐户。 我的问题是,当他们输入自己的详细信息,然后创建帐户时,文本文件中没有添加任何内容

文本文件的格式如下“username:password““

没有语法错误发生


Tags: txtinputyourifusernamepasswordopenyes