为什么我的代码似乎跳过了所有一半的功能?

Why is my code seemingly skipping all half of the function?

我正在创建一个系统,它会检查用户是否已经申请了用户名,但是在创建帐户时它会询问他们想要的用户名,然后将其发送回选项屏幕以询问他们是否想要登录或创建帐户。

def user_create():
    f = open("users.txt", "a")
    unique = 0
    name = input("Enter a username: ")
    with open("users.txt", "r") as file:
        file_read = csv.reader(file)
        filerow = 1
        for row in file_read:
            if row[0] == name:
                print("This username is already taken, try another one")
                file.close()
                unique = False
                break
            else:
                unique = True
                filerow = filerow + 1
    if unique == True:
        file.close()
    elif unique == False:
        signin1()
    else:
        pass
    if name.replace(" ", "").isalpha():     
        print("Name is valid")
    else:
        print ("Name is invalid")
        user_create()
    password = input("Enter a password: ")
    f.write(name+","+password+",\n")
    f.close()
    print("Account created")
    signin1()

输出:

Enter a username: testuser
1. Login
2. Create
Which one would you like to do? 

接下来应该询问用户密码,但似乎只跳过了一半代码..

所有登录码:

def signin1():
    print ("1. Login")
    print ("2. Create")
    choice = input("Which one would you like to do? ")
    if choice == "1":
        existence = os.path.exists("users.txt")
        if existence == False:
            print("There are no authorised accounts yet created")
            signin1()
        else:
            mainsignin()
    elif choice == "2":
        user_create()
    else:
        print("Invaild choice")
        signin1()

    def user_create():
        f = open("users.txt", "a")
        unique = 0
        name = input("Enter a username: ")
        with open("users.txt", "r") as file:
            file_read = csv.reader(file)
            filerow = 1
            for row in file_read:
                if row[0] == name:
                    print("This username is already taken, try another one")
                    file.close()
                    unique = False
                    break
                else:
                    unique = True
                    filerow = filerow + 1
        if unique == True:
            file.close()
        elif unique == False:
            signin1()
        else:
            pass
        if name.replace(" ", "").isalpha():     
            print("Name is valid")
        else:
            print ("Name is invalid")
            user_create()
        password = input("Enter a password: ")
        f.write(name+","+password+",\n")
        f.close()
        print("Account created")
        signin1()

        def mainsignin():
        f = open("users.txt", "w")
        f.close
        with open("users.txt", "r") as file:
            file_reader = csv.reader(file)
            user_find(file_reader)

def user_find(file):
    user = input("Enter your username: ")
    filerow = 1
    login = False
    for row in file:
        if row[0] == user:
            print("Username found:", user)
            user_found = [row[0], row[1]]
            login = True
            break
        else:
            filerow = filerow + 1
        if login == True:
            global user1
            user1 = user
            pass_check(user_found)
        else:
            print ("Could not find user with the name: "+str(user))
            signin1()

def pass_check(user_found):
    userpass = input("Enter your password: ")
    if user_found[1] == userpass:
        print("password match")
        str(user_found)
        game_login_user2()
    else:
        print("password does not match")
        mainsignin()

您的问题源于这种情况:elif unique == False:

您首先将值 0 分配给 unique,然后根据您的文件内容将 TrueFalse 分配给它。如果你的文件是空的,那么 unique 在文件循环的末尾仍然会有一个 0。现在,这里可能令人惊讶的是 0 == False 被评估为 true!这是因为 0 是“Falsy”(有关详细信息,请参阅 )。因此,当您期望代码转到 else 时,实际上它又回到了 signin1.

为避免将来出现此类错误,我强烈建议使用 x is Truex is False 代替 x == Truex == False。我还建议您不要在同一个变量中使用不同的类型(int、bool),因为这会导致像这样的事故。

最后但同样重要的是,我建议您学习如何使用调试器。如果您逐行 运行 这段代码,您会很快发现这个问题。

我不确定我是否正确理解了你的问题,问题似乎在 user_create 函数中,unique 设置为 0 (False)在此函数的最开始,如果 users.txt 恰好为空,即使给出了唯一的用户名,也会调用 signin1

def user_create():
    f = open("users.txt", "a")
    unique = 0  # unique is set to False
    name = input("Enter a username: ")
    with open("users.txt", "r") as file:
        file_read = csv.reader(file)
        filerow = 1
        for row in file_read: # if users.txt is empty, following 8 lines won't run
            if row[0] == name:
                print("This username is already taken, try another one")
                file.close()
                unique = False
                break
            else:
                unique = True
                filerow = filerow + 1
    if unique == True:
        file.close()
    elif unique == False:
        signin1() # and signin1 would be called here even if a unique username is given
    else:
        pass