【发布时间】:2020-10-11 17:28:47
【问题描述】:
UsernameFile = open("C:/Ishaan's Folder/Homework (Year 11)/Computing/NEA Programming Project/Usernames.txt", "r")
PasswordFile = open("C:/Ishaan's Folder/Homework (Year 11)/Computing/NEA Programming Project/Passwords.txt", "r")
ScoresFileIshaanFolder = open("C:/Ishaan's Folder/Homework (Year 11)/Computing/NEA Programming Project/Scores.txt", "a")
PlayerOneAuthorised = False
PlayerTwoAuthorised = False
#Read lines until we reach the place in the file that we want.
UsernameLine = UsernameFile.readline()
PasswordLine = PasswordFile.readline()
#To check whether the Player One is authorised
print("Player One Authentication.")
Username1 = str(input("Please enter your username: "))
print(Username1)
while UsernameLine != "":
UsernameLine = UsernameFile.readline()
if Username1 in str(UsernameLine):
Password1 = input("""Username authorised
Please enter your password: """)
while PasswordLine != "":
PasswordLine = PasswordFile.readline()
if Password1 in str(PasswordLine):
print("Password Authorised")
print(Username1, " is now authorised to play this game.")
PlayerOneAuthorised = True
else:
NewPassword1 = input("""Password not authorised
Please re-enter your password: """)
break
else:
newUsername1= input("""Username not authorised.
Please re-enter your username: """)
Username1 = newUsername1
if newUsername1 in str(UsernameLine):
Password1 = input("""Username authorised
Please enter your password: """)
while PasswordLine != "":
PasswordLine = PasswordFile.readline()
if Password1 in str(PasswordLine):
print("Password Authorised")
print(Username1, "is now authorised to play this game.")
PlayerOneAuthorised = True
else:
NewPassword1 = input("""Password not authorised
Please re-enter your password: """)
if NewPassword1 in str(PasswordLine):
print("Password Authorised")
print(Username1, "is now authorised to play this game.")
PlayerOneAuthorised = True
break
else:
print("""Username not authorised.
You are not allowed to play this game.""")
input("Press enter to exit")
exit()
break
#To check whether Player Two is authorised
print("Player Two Authentication")
Username2 = str(input("Please enter your username: "))
while UsernameLine != "":
UsernameLine = UsernameFile.readline()
if Username2 in str(UsernameLine):
Password2 = input("""Username authorised
Please enter your password: """)
while PasswordLine != "":
PasswordLine = PasswordFile.readline()
if Password2 in str(PasswordLine):
print("Password Authorised")
print(Username2, "is now authorised to play this game.")
PlayerTwoAuthorised = True
else:
NewPassword2 = input("""Password not authorised
Please re-enter your password: """)
break
else:
newUsername2= input("""Username not authorised.
Please re-enter your username: """)
Username2 = newUsername2
if newUsername2 in str(UsernameLine):
Password2 = input("""Username authorised
Please enter your password: """)
while PasswordLine != "":
PasswordLine = PasswordFile.readline()
if Password2 in str(PasswordLine):
print("Password Authorised")
print(Username2, "is now authorised to play this game.")
PlayerTwoAuthorised = True
else:
NewPassword2 = input("""Password not authorised
Please re-enter your password: """)
if NewPassword2 in str(PasswordLine):
print("Password Authorised")
print(Username2, "is now authorised to play this game.")
PlayerTwoAuthorised = True
break
else:
print("""Username not authorised.
You are not allowed to play this game.""")
input("Press enter to exit")
exit()
break
这是用户名文件:
Ishaan
Brandon
Harvey
这是密码文件:
Ishaan
Brandon
Harvey
当我输入“Ishaan”作为用户名和密码时,它正在被授权,但是当我输入“Brandon”或“Harvey”时,它说这些用户名不在文件中。
我认为这是读取文件的问题。 我已经尝试尽我所能。我的朋友告诉我添加一个函数而不是单独创建它,但我对函数没有那么自信。因此,如果您认为自己知道答案,请发表评论。
【问题讨论】:
-
请在您的调试器中启动您的代码,并逐行浏览您的代码,直到您可以定位问题。创建minimal reproducible example 并发布。不要放弃你的整个项目并期望其他人调试它。您还应该删除用户输入并将其替换为值。
-
UsernameFile未在您的代码中定义。 -
您甚至没有检查文件中的所有用户名。您读取第一个用户名并将其与输入进行比较。用户名和密码之间没有联系。所有用户都可以使用所有密码。我认为重新开始比修复这个项目更容易。你应该从笔和纸开始。画出程序流程。
-
@ThomasSablik All Ineed 是读取文件中所有用户名的函数。我还在上面忘记的代码中添加了 UsernameFile
标签: python python-3.x