我的程序随机收到“无”?

时间:2013-04-10 22:35:35

标签: python python-3.x

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)

但我并不积极。我只想尝试从每个回合的字母列表中删除用户的字母猜测。提前谢谢。

1 个答案:

答案 0 :(得分:4)

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

简而言之,只需使用:

letters.remove(letter_guess)