我最近开始学习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))
答案 0 :(得分:0)
这是新代码:
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")