从txt文件中获取字符串、变量,并在主脚本中使用

2024-09-30 03:24:13 发布

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

我最近开始学习Python。 这里我有一个简单的注册/登录系统,它执行并将数据保存到TXT文件中。所以在这个“保存”系统之后,我使用f.write()函数,我怎么才能得到保存的文件,比如密码,钱,gmail地址?在把Ballance=500.00的变量放入新帐户后,在Register函数中,我确实得到了更多未来脚本的错误,就是许多变量、字符串没有定义。那么,如何从TXT文件中“提取”字符串和变量,并在主脚本中使用呢? 我的注册/登录代码,你也可以说,如果里面有任何问题的代码

global username1, password2, ballance, username2, password2
check = True
Login_Register = input("Welcome,\nType L for Login, R to Register\n")
if Login_Register == "l" or Login_Register =="L":
    while check:
        with open(ban_list, mode='r', encoding='utf-8') as f:
            username1 = input("Enter your username: ")
            password1 = getpass.getpass("Enter your password: ")
            for line in f:
                if("Username:"+username1+" Password:"+password1) == line.strip():
                    print("you are logged in")
                    check = False
                    break;
                else:
                    check = False
                    print("Username or password does not exist")
                    continue

elif "r" in Login_Register or "R" in Login_Register:
 while True:
     try:
            ballance = 500.00
            f = open(ban_list, mode='a+')
            username2 = input("~ Please enter your Username!\n")
            password2 = getpass.getpass("~ Please enter your password!\n")
            Gmail = input("~ Please add your Email address!\n")
            f.write(f"\nUsername:{username2} Password:{password2} Gmail:{Gmail} Ballance:{ballance}\n")
            f.close()
            print("username and password has been made")

            break;
     except ValueError:
        print('* Not a value !')

print("WElcome {} ".format(username1))

Tags: or文件inregisterinputyourchecklogin
1条回答
网友
1楼 · 发布于 2024-09-30 03:24:13

这是新代码:

global username1, password2, ballance, username2, password2
check = True
Login_Register = input("Welcome,\nType L for Login, R to Register\n")
if Login_Register == "l" or Login_Register =="L":
    while check:
        with open('accountfile.txt','r') as f:
            username1 = input("Enter your username: ")
            password1 = input("Enter your password: ")
            for line in f:
                text = line.strip().split()
                if username1 == text[1] and password1 == text[3]:
                    print("you are logged in")
                    check = False
                    print("Welcome", username1)
                    break;
                else:
                    print("Username or password does not exist")
                    break;

elif "r" == Login_Register or "R" == Login_Register:
    ballance = 500.00
    f = open('accountfile.txt','a+')
    username2 = input("~ Please enter your Username!\n")
    password2 = input("~ Please enter your password!\n")
    Gmail = input("~ Please add your Email address!\n")
    f.write(f"\nUsername: {username2} Password: {password2} Gmail: {Gmail} Ballance: {ballance}\n")
    f.close()
    print("username and password has been made")

相关问题 更多 >

    热门问题