我需要实现二进制搜索算法,然后将输入的8个随机字母与文本文件(列表)进行比较

所以我必须比较使用文本文件中的二进制搜索和输入的8个字符随机字母,然后如果文本文件(列表)和随机字母中都没有输入的单词,那么我将进行3次试验,否则它将继续进行游戏 到目前为止,这是我已经实现的代码:

def PlayGame(guess_word):

print("Game Starts Now")
random_words = GetRandomLetters()
print( " Your Phrase: ", random_words)

secret_word = "hello"
tries = 3
while tries > 0:
    guess_word = input("Guess a word: ")
    if guess_word != secret_word:
        tries -= 1
        print("Sorry, word not found, you have", tries, "tries left")
        if tries == 0:
            print("Ran out of tries!")
            break
    else:
        print("Congrats, you found the word !!!")
        PlayGame()
        break

def BinarySearch():
array = GetJugWords()
target = input("Enter a word: ")
first = 0
last = len(array) - 1

while first <= last:
    mid = (first + last) // 2
    midpoint = array[mid]
    if midpoint > target:
        last = mid - 1
    elif midpoint < target:
        first = first + 1
    else:
        return midpoint

def GetRandomLetters():        
letters = string.ascii_lowercase
random_letters = ''.join(random.choice(letters) for i in range(8))
return random_letters

def GetJugWords():
words_list = []
with open('jug_words.txt') as file:
    for line in file:
        line = line.strip()
        words_list.append(line)
return words_list

if __name__ == '__main__':
PlayGame()
评论