Python 使用二进制搜索和递归搜索文本文件

Python 使用二进制搜索和递归搜索文本文件,python,file,sorting,search,Python,File,Sorting,Search,我想使用递归和二进制搜索来搜索文本文件中的用户名和密码。每个帐户(用户名和密码)保存在一行中,两行之间用空格隔开 这就是我目前拥有的(确实有效),我只想使用更多的技术: def validation(username1, password1): file = open("Accounts.txt", "r") for line in file: line = line.split("\n")

我想使用递归和二进制搜索来搜索文本文件中的用户名和密码。每个帐户(用户名和密码)保存在一行中,两行之间用空格隔开

这就是我目前拥有的(确实有效),我只想使用更多的技术:

def validation(username1, password1):
    file = open("Accounts.txt", "r")
    for line in file:
        line = line.split("\n")
        line = line[0].split(" ")

        if username1 == line[0] and password1 == line[1]:
            main_menu()     #main_menu() will allow the user to enter the game
        else:
            valid = Label(root, text="Account not found try again or sign up")
            valid.place(x=215, y=130)
            login()        #will recall the login window and ask them to renter details 
    file.close()
我想使用以下代码:

def validation(username1, password1):
    file = open("Accounts.txt", "r")
userame1和passwrod1是用户输入的详细信息。现在我想检查是否已经使用二进制搜索和递归创建了一个帐户


艾米:你有什么想法吗?我仍在学习如何编码。

您可以尝试查看文本文件中是否有特定单词:

def validation(username1, password1):
    file = open("Accounts.txt", "r")
    content = file.read()

    if str(username1) and str(password1) in content:
        return True

    else:
        return False

然后根据验证结果调用main_menu()或login()

最简单的方法是读取所有行并对其排序,以便应用二进制搜索

def验证(用户名1,密码1):
文件=打开(“Accounts.txt”、“r”)
lines=[i.strip().split(),用于文件中的i.readlines()]
#行是[用户名、密码]子列表的列表
line.sort()
如果二进制搜索(行[username1,password1]):
登录()
其他:
打印(“未找到用户”)

您的要求仍然不清楚,您提到了二进制、递归和冒泡排序?@AbhinavMathur很抱歉,我更改了问题xRead all line,对列表排序,然后使用二进制搜索?有什么问题吗here@AbhinavMathur你能给我一个电话吗example@Katie010203二进制搜索和递归似乎都与此任务无关。如果您想使用另一种方法,我建议您创建一个dict,其中用户名作为键,密码作为值。然后,如果用户名不在生成的dict中,您的程序将提示创建一个新帐户;但是如果是,程序将检查密码,如果密码错误,则提示用户(使用while循环)输入正确的密码。这是否使用递归和二进制搜索?我不会说谎,我完全不知道。但我想说yes@persephone不,没有。请先阅读术语“二进制搜索”是一种功能吗?x@Katie010203请参阅python stdlib中的。@ekhumoro谢谢