我正在用Python 3做游戏。我需要查看玩家输入的用户名和密码是否正确。用户名和密码位于文件中,用户注册后将放置在该文件中。
def login_signup():
loginsignup = input("Do you want to login or sign up? Enter L/S: ")
if loginsignup == "S":
userpassA = open("USERPASS.txt.txt", "a")
user = input("Please enter a username: ")
password = input("Please enter a password: ")
userpassA.write(user)
userpassA.write("\n")
userpassA.write(password)
userpassA.close()
if loginsignup == "L": # Here is where it needs to be.
userpassR = open("USERPASS.txt.txt", "r")
lines = userpassR.readline()
username = input("Username: ")
# passwordl = input("Password: ")
if username == lines[0]:
print("Right.")
userpassR.close()
答案 0 :(得分:0)
每次调用 userpassR.readline()时,文件的下一行将被读取,而不是一次读取所有行。假设文件“ USERPASS.txt.txt”中的内容为
if (c + 1.0):
k = (c - 1.0) / (c + 1.0)
else:
k = 0
,第一次调用 userpassR.readline()时,将返回字符串user1
mypswd
。下次调用它时,返回值为'user1\n'
。
因此,要修改您的代码:
'mypswd'