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
当我注册一个帐户时,它不会显示在文件中。另外,当我使用手动添加的有效帐户登录时,它仅返回“无登录” –