运行代码时出现逻辑错误,它继续抛出第一个 if 语句错误“错误!用户名不存在。” 我需要能够以管理员身份登录,然后通过将用户添加到 .txt 文件来添加用户,之后,如果程序再次运行,我可以通过管理员或在 txt 中创建的新用户之一登录文件。我似乎无法正确拆分它,以便循环在登录时正确地遍历列表。
例子:
print(new_lines) = [['admin', 'adm1n'], ['kevin', 'kev1n'], ['dorothy', '1234']]
.txt 文件内容,每个条目在新行 = admin,adm1n\n kevin,kev1n\n dorothy,1234
到目前为止的代码:
import time
#User input of username and password
user_name = input("Username:\n")
user_pass = input("Password: \n")
#Opening document
with open("user.txt", "r+", encoding = "utf-8-sig") as f:
new_lines = []
for line in f:
new_line = line.strip()
new_lines.append(new_line.split(","))
print(new_lines)
#Loop to enter user name and password
for x in new_lines:
for y in x:
if user_name != new_lines[:][0]:
print("Error! Username does not exist.")
user_name = input("Username:\n")
user_pass = input("Password: \n")
elif user_pass != new_lines[:][1]:
print("Error! Incorrect password.")
user_name = input("Username:\n")
user_pass = input("Password: \n")
else:
print("Welcome back!")
break
break
#User options to choose from
user_choice = input("""\nPlease select one of the following options:
\nr - register user
\na - add task
\nva - view all tasks
\nvm - view my tasks
\ne - exit
\nAnswer: """)
qq_遁去的一_1
慕妹3146593
相关分类