【问题标题】:Can someone suggest why the second player is not being authorised?有人可以建议为什么第二个玩家没有被授权吗?
【发布时间】: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


【解决方案1】:

这个任务的代码太多了。您需要更好地构建您的编码 - 将其分开在任务中。您永远不会阅读所有用户名和密码。

您需要构建一个包含用户和密码的数据结构,因为您需要逐行读取这两个文件并“连接”这些行。

如果您需要检查登录,您只需使用您构建的结构并进行查找。 “需要登录代码”部分根本不需要处理文件读取。

创建密码和用户文件:

with open ("user.txt","w") as u, open("pass.txt","w") as p:
    for num in range(3):
        u.write(f"user{num+1}\n")
        p.write(f"pass{num+1}\n")

读入文件并存入字典:

# read all users/pw into a dictionary
users = {}
# open both files
with open("user.txt") as u, open("pass.txt") as p:
    # connect the lines using zip() with pairs the lines into tuples
    for (user,pw) in zip(u,p):
        # set the user/pw in your dictionary
        users[user.strip()] = pw.strip()

# don't print in your application     
print(users)

输出:

{'user1': 'pass1', 'user2': 'pass2', 'user3': 'pass3'}

使用字典的程序:

您可以轻松检查用户是否可以登录:

while True:
    user = input("Enter user name (empty user to quit): ")
    if not user:  
        break
    # never explain that this user is not present, ask for pw and
    # quite with a vague message that either user or pw wrong for
    # "more" security
    pw = input("Enter passphrase: ")

    # look if that key is in the dict and if so if the passphrase matches
    if users.get(user) == pw:
        print("Logged in.")
        break
    else:
        print("Invalid user.")
if not user:
    print("Quitting")
    exit(1)
else:
    print("Gooooooo")
    # logged in

【讨论】:

  • 我收到一条错误消息:“TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper”发生这种情况时我正试图将文件读入字典
  • @Ishaan 我的代码没有给出这个错误。如果你试图改变我的,你的可能。调试它,如果它不起作用,请提出一个新问题,请将代码中有问题的部分发布为minimal reproducible example
  • 如何更改您的代码以适合我的?我已经使用其中的名称创建了一个用户名和密码文件
  • @Ishaan 你在用"C:/Ishaan's Folder/Homework (Year 11)/Computing/NEA Programming Project/Usernames.txt" 阅读时替换'user.txt' 并同样替换你的密码文件?在# open both file - with open("user.txt") as u, open("pass.txt") as p: 行中?
  • 我刚刚把你的代码放进去,非常感谢!
猜你喜欢
  • 2022-08-07
  • 1970-01-01
  • 1970-01-01
  • 2018-12-25
  • 1970-01-01
  • 2022-11-13
  • 1970-01-01
  • 1970-01-01
  • 2015-02-21
相关资源
最近更新 更多