Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python说一个特定的项目在一个空的列表中_Python - Fatal编程技术网

Python说一个特定的项目在一个空的列表中

Python说一个特定的项目在一个空的列表中,python,Python,我正在尝试制作一个刽子手游戏。我有一个功能,如果你选择的字母在所有猜测的字母列表中,它会告诉你它已经被猜测了。当我运行这个程序时,它会说这个字母已经猜到了,即使它没有被猜到,或者它是我猜到的第一个字母 import random print("---Hangman---") WordList = ["programming", "computer", "game"] LetterList = [] Word = ra

我正在尝试制作一个刽子手游戏。我有一个功能,如果你选择的字母在所有猜测的字母列表中,它会告诉你它已经被猜测了。当我运行这个程序时,它会说这个字母已经猜到了,即使它没有被猜到,或者它是我猜到的第一个字母

import random
print("---Hangman---")
WordList = ["programming", "computer", "game"]
LetterList = []
Word = random.choice(WordList)
NumberOfLetters = len(Word)
print("There are", NumberOfLetters, "letters in the word.")


for i in range (NumberOfLetters):
    print("Guess a letter:")
    LetterGuess = input()
    LetterList.append(LetterGuess)
    if LetterGuess in LetterList:
        print("You already guessed that letter")
    elif LetterGuess in Word:
        print("Correct")
        print("You now have the correct letters: ", LetterList)
        i += 1
    elif LetterGuess not in Word:
        print("Wrong")
        print("You have the correct letters: ", LetterList)
当我猜一个数字时,它是这样说的:

 ---Hangman---
There are 11 letters in the word.
Guess a letter:
d
You already guessed that letter

您将猜测添加到列表中,然后立即检查它是否存在

LetterList.append(LetterGuess)
if LetterGuess in LetterList:

好的,我现在明白了,谢谢!