1
def validateGuess():
    print(letters)
    inputGuess= input("Choose one of the letters above:")
    if len(inputGuess)> 1:
        inputGuess=input("Choose ONE of the letters above:")
return inputGuess

def wordLength():
    length=input("Welcome to Hangman. How many letters in the secret word?")
    length1=int(length)
    if length1 <0:
        length=input("Welcome to Hangman. How many letters in the secret word?")
    return length    

def guessAmount():
    guesses=input("How many guesses would you like?")
    guesses1=int(guesses)
    if guesses1 < 0:
         guesses=input("How many guesses would you like?")
    return guesses

welcome=wordLength()
remain=input("Do you want to print the count of how many words remain [Y/N]?")
guessNumber=int(guessAmount())
F=open('dictionary.txt')
F1=F.readlines()


welcome2=int(welcome)
a_list=[word.strip('\n') for word in F1]
possible_words = [x for x in a_list if len(x) == welcome2]

letters=      ['a','b','c','d','e','f','g','h','i','j','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
while guessNumber > 0:
    letter_guess=validateGuess()
    guessNumber -= 1
    letters=letters.remove(letter_guess)

基本上这是我正在创建的邪恶刽子手游戏的开始,但我遇到了一个我以前从未见过的问题。每当我运行程序时,每当我到达我的 while 循环的字母猜测部分时,我都会输入一个字母,例如 b,并且由于某种原因它报告回“无”。我相信这与

letters=letters.remove(letter_guess)

但我并不积极。我只是想从每轮的字母列表中删除用户的字母猜测。提前致谢。

4

1 回答 1

4

list.remove()就地运作。也就是说,它修改了它被调用的对象,它不会返回没有值的新列表。

简而言之,只需使用:

letters.remove(letter_guess)
于 2013-04-10T22:38:17.220 回答