私は最近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))
これは新しいコードです:
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")
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加