如何检查列表的前 7 个元素以打印一些文本?

How do I check against the first 7 elements of the list in order to print some text?

从数组中,我尝试使用具有 3 个条件的 if 语句 - 如果尚未正确猜出从 'wordlist' 列表中选择的单词并且已经进行了 7 次以上的尝试,我希望打印一些提示文本。由于未知原因,提示文本总是在第 7 次尝试时显示,无论所选单词是哪个索引。

# Create a list of hangman words
  wordList = ["cat","dog","mouse", "giraffe", "otter", "shark", "sheep", "car", "motorbike",
  "bus", "aeroplane", "pizza", "chips", "cheese"]

# Choose a word from the list at random
  wordChosen = random.choice(wordList)

# Keep asking the player until all letters are guessed
  while display != wordChosen:
    guess = input(str("Please enter a guess for the {} ".format(len(display)) + "letter word: "))#[0]
    guess = guess.lower()
    #Add the players guess to the list of used letters
    used.extend(guess)
    print ("Attempts: ")
    print (attempts)

    print(wordChosen) # Added for testing

    # Provide a hint if unsuccessful after 7 attempts
    if attempts >= 7 and guess != wordChosen and wordChosen[0:6]:
      print("HINT: It's an animal")

在这种情况下,我只希望索引 0:7 产生此文本。例如,如果从随机函数中选择 'car',文本仍然显示我不想要的内容。

我尝试使用 numpy,但使用以下方法似乎更接近解决方案:

# Provide a hint if unsuccessful after 7 attempts
        if attempts >= 7 and guess != wordChosen and wordChosen[0:6]:
          print("HINT: It's an animal")

您没有在倒数第二行中引用 wordList 进行比较:

# Create a list of hangman words
  wordList = ["cat","dog","mouse", "giraffe", "otter", "shark", "sheep", "car", "motorbike",
  "bus", "aeroplane", "pizza", "chips", "cheese"]

# Choose a word from the list at random
  wordChosen = random.choice(wordList)

# Keep asking the player until all letters are guessed
  while display != wordChosen:
    guess = input(str("Please enter a guess for the {} ".format(len(display)) + "letter word: "))#[0]
    guess = guess.lower()
    #Add the players guess to the list of used letters
    used.extend(guess)
    print ("Attempts: ")
    print (attempts)

    print(wordChosen) # Added for testing

    # Provide a hint if unsuccessful after 7 attempts
    if attempts >= 7 and guess != wordChosen and wordChosen in wordList[0:6]:
      print("HINT: It's an animal")

而不是:

if attempts >= 7 and guess != wordChosen and wordChosen[0:6]:

你需要:

if attempts >= 7 and guess != wordChosen and wordChosen in wordList[0:6]: