python - 如何立即更新用户名而不是重新运行程序
问题描述
该程序用于检查用户名和密码以允许用户进入。但是,在我添加新用户的最后,我希望用户能够立即尝试导入他们新创建的用户名和密码,以便他们可以输入程序,在将用户名和密码添加到数据库后无需重新运行。但是,此代码返回错误“列表索引超出范围”
第 1 阶段:打开文件并获取数据
filename1 = "c:\Users\Anna Hamelin\Documents\Python Scripts\SourceCode\Project2\usernames.txt" 文件 = open(filename1, "r")
#Usernames
users = file.read()
usernameslist = [line.strip() for line in open("c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\usernames.txt")]
#print(users) #Check file
#print(usernameslist) #Check usernames list
file.close()
filename2 = "c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\passwords.txt"
file = open(filename2, "r")
#Passwords
passwords = file.read()
passwordslist = [line.strip() for line in open("c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\passwords.txt")]
#print(passwords) #Check file
#print(passwordslist) #Check passwords list
file.close()
#Compile the usernames and passwords lists for easy checking
#compiled_list = list(zip(usernameslist,passwordslist))
#print(compiled_list)
#Scores
filename3 = "c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\scores.txt"
file = open(filename3, "r")
scores = file.read()
scoreslist = [line.strip() for line in open("c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\scores.txt")]
#print(scores) #Check file
#print(scoreslist) #Check scores
file.close()
#STAGE 2
#Print Welcome/Intro message
response = input("-"*50 + "\nWelcome! Do you have an account (y/n)? ")
print("-"*50)
#If user has an account:
if response == "y":
#Create a login function:
#def login():
goodlogin = False
username = input("Please enter your username: ")
password = input("Please enter your password: ")
for id in range(len(usernameslist)):
if username == usernameslist[id] and password == passwordslist[id]:
goodlogin = True
if goodlogin:
print(print_in_green + "Access granted!" + print_default)
else:
print(print_in_red + "Incorrect Login credentials, please try again." + print_default)
#login()
#If user does not have account:
else:
newusername = input("What is your new username? ")
newpassowrd = input("What is your new password? ")
file = open(filename1, "a")
file.write("\n" + newusername)
file = open(filename2, "a")
file.write("\n" + newpassowrd)
file.close
print("Now that you have created an account, please continue.")
goodlogin = False
username = input("Please enter your username: ")
password = input("Please enter your password: ")
for id in range(len(usernameslist)):
if username == usernameslist[id] and password == passwordslist[id]:
goodlogin = True
if goodlogin:
print(print_in_green + "Access granted!" + print_default)
else:
print(print_in_red + "Incorrect Login credentials, please try again." + print_default)
解决方案
我添加了几个函数来减少代码重用。我还在 login_user 函数中加载了用户名和密码,所以它总是得到最新的副本。
users_path = "c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\usernames.txt"
passwords_path = "c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\passwords.txt"
scoreslist_path = "c:\\Users\\Anna Hamelin\\Documents\\Python Scripts\\SourceCode\\Project2\\scores.txt"
def get_file_contents(file_path):
return [line.strip() for line in open(file_path)]
scoreslist = get_file_contents(scoreslist_path)
def add_file_contents(file_path, contents):
with open(file_path, "a") as file:
file.write(contents)
def login_user(new_account=False):
usernameslist = get_file_contents(users_path)
passwordslist = get_file_contents(passwords_path)
if new_account:
response = 'y'
else:
response = input("-"*50 + "\nWelcome! Do you have an account (y/n)? ")
print("-"*50)
#If user has an account:
if response == "y":
goodlogin = False
username = input("Please enter your username: ")
password = input("Please enter your password: ")
for id in range(len(usernameslist)):
if username == usernameslist[id] and password == passwordslist[id]:
goodlogin = True
if goodlogin:
print("Access granted!")
else:
print("Incorrect Login credentials, please try again.")
#If user does not have account:
else:
newusername = input("What is your new username? ")
newpassword = input("What is your new password? ")
add_file_contents(users_path, '\n' + newusername)
add_file_contents(passwords_path, '\n' + newpassword)
login_user(new_account=True)
login_user()
推荐阅读
- haskell - 为什么 MVector 不可折叠?
- sql - 在包含数字 1 到 1000 的视图中创建临时表
- python - 显示单个字符的 Python 列表元素(来自 CSV 的 Pandas DF)
- python - 使用 dict 设置散景图属性
- ios - “for-in 循环需要 '[String]?' 符合'Sequence';你的意思是解开可选的吗?” 但我不认为我在使用选项
- javascript - React/Ionic:尝试发出 Axios 请求时,来自数据库的数据显示未定义
- r - 错误消息:重命名行名时“row.names”长度无效
- rsync - Mac OS:如何使用 RSYNC 复制过去 24 小时内修改的文件并保持文件夹结构?
- sql - 在 SQL 中创建一个显示变量百分比的表
- android - 为什么 TouchListener 不在自定义警报对话框上工作?