Python 读/写到文件登录脚本



我想知道是否有人会帮忙。我是蟒蛇的新手。我正在尝试为游戏创建一个基本的登录脚本,该脚本会将用户名和密码写入文本文件。登录时,它将从该文本文件中读取并比较用户所做的条目。代码如下:

def Register():
print("Hello! You need to register an account before you can begin")
username = input("Please enter a username: ")
password = input("Now please enter a password: ")
file = open("Login.txt","a")
file.write (username)
file.write (",")
file.write (password)
file.write("n")
file.close()
print ("Your login details have been saved. ")
print("You will now need to login")
Login() 
def Login():
print("Please enter your details to log in")
username1 = input("Please enter your username: ")
password1 = input("Please enter your password: ")
file = open("Login.txt","r")
for row in file:
field = row.split(",")
username = field[0]
password = field[1]
lastchar = len(password)-1
password = password[0:lastchar]
print(username,password)
if username1 == username and password1 == password:
print("Hello",username)
else:
print("incorrect")
#file.close()

user=input("Are you already a user? ")
if user == "Yes":
Login()
elif user =="No":
Register()
print("Welcome to our game")

我已经输入了存储在文本文件中的第二个用户,它似乎正在工作,但它检查我的第一个条目并说它不正确,然后循环到第二个条目。这是我不断得到的输出:

Are you already a user? Yes
Please enter your details to log in
Please enter your username: jen
Please enter your password: ben
tess bess
incorrect
jen ben
Hello jen
Welcome to the dice game
>>> 

有没有人知道如何只显示您输入的条目?

谢谢

就像Sharku说的那样,把打印(用户名,密码)放在你的if下面。在用户输入后也清楚地写下用户名和密码并不是一个真正的智能moove,删除它,并在用户登录时让您的消息!

for row in file:
field = row.split(",")
username = field[0]
password = field[1]
lastchar = len(password)-1
password = password[0:lastchar]
if username1 == username and password1 == password:
print("Hello",username)
else:
print("incorrect")

正如sytech所说,你可以使用for循环的breakelse子句:

for row in file:
field = row.split(",")
username = field[0]
password = field[1]
lastchar = len(password)-1
password = password[0:lastchar]
if username1 == username and password1 == password:
print("Hello",username)
break
else:
print("incorrect")

您的"for loop"将遍历文件中的每个条目,这意味着对于文件中的每个条目,您的 for 循环会打印该条目,因为这一行:

print(username,password)

如果不希望它打印文件中的所有值,请删除此行代码。 正如其他人所建议的那样,在 if 语句中添加"break"将意味着一旦您的循环找到与用户输入的条目匹配的条目,它就会离开循环,而不会继续不必要地遍历所有值。

你可以做这样的事情:

if username1 == username and password1 == password:
print("Hello",username)
break
else:
continue

这意味着当用户输入与文件中的条目不匹配时,循环将继续,直到找到匹配项。 但是,如果用户不存在,则代码不会考虑。

import os.path
if not os.path.exists('register.txt'):
file = open('register.txt', 'w')
file.close()

def register():
username = input('Enter username: ')
if username in open('register.txt', 'r').read():
print('Username already exists')
exit()
password = input('Enter password: ')
c_password = input('Enter confirm password: ')
if password != c_password:
print('Sorry password not match')
exit()
handle = open('register.txt', 'a')
handle.write(username)
handle.write(' ')
handle.write(password)
handle.write('n')
handle.close()
print('User was successfully registered')
exit()

def login():
username = input('Enter username: ')
password = input('Enter password: ')
get_data = open('register.txt', 'r').readlines()
users_data = []
for user in get_data:
users_data.append(user.split())
total_user = len(users_data)
increment = 0
login_success = 0
while increment < total_user:
usernames = users_data[increment][0]
passwords = users_data[increment][1]
if username == usernames and password == passwords:
login_success = 1
increment += 1
if login_success == 1:
print('Welcome ' + username)
else:
print('invalid username & password')

question = input('Do you have an account?/yes/no')
if question == 'yes':
login()
else:
register()

最新更新