1
我寫了一個python腳本來記錄用戶並註冊。它使用一個txt文件來存儲用戶名和密碼。我寫在http://trinket.io。但是,它在普通的python中不起作用。任何人都可以告訴我需要改變以解決問題嗎? 編輯: 這裏是代碼如何修復使用txt文件登錄並註冊用戶的python登錄腳本
file = open('accounts.txt', 'a+')
lines = file.readlines()
login = {}
for line in lines:
key, value = line.strip().split(', ')
login[key] = value
while True:
command = input('$ ')
command_list = command.split(' ')
if command_list[0] == 'login':
username = command_list[1]
password = command_list[2]
try:
if login[username] == password:
print('login')
else:
print('no login')
except KeyError:
print('no login')
elif command_list[0] == "register":
file.write("\n")
file.write(command_list[1])
file.write(", ")
file.write(command_list[2])
elif command_list[0] == "help":
print("""To login, type login, then type the username and then type the password.
To register, type register, then type the username and then the password.""")
elif command_list[0]== "quit":
break
else:
print('unrecognised command')
請問您是否更具體?哪部分不按預期工作? – Windmill
當我註冊一個帳戶時,它不會顯示在文件中。另外,當我使用手動添加的有效帳戶登錄時,它僅返回「無登錄」 –