Python刽子手错误

时间:2016-03-24 21:17:17

标签: python

我正在做一个刽子手。这是我的代码:

from random import choice


word = choice(["gpu", "cpu","motherboard","alu","cache","ram","hardware","hosted software","monitor","keyboard"])
word_len = len(word)
guessed = []
wrong = 6
space = word.find(" ")!=-1
while True and wrong > 0:
    out = ""
    if space == True:  #somewords have a space so i did this to 
        guessed =" "   #put the space in there and reduce the length by one
        word_len -=1   # it works fine without this.
    for letter in word:
        if letter in guessed:
            out = out + letter
        else:
            out = out + "_"
    if out == word:
        print("You guessed", word)
        break
    print("Guess the ", word_len, "letter word:", out)
    guess = input()
    if guess in guessed:
        print("Already guessed", guess)
    elif guess in word:
        print("Yay")
        guessed.append(guess)
    else:
        print("Nope")
        wrong = wrong - 1
        print("You have ", wrong," attempts left.")
        if wrong == 0:
            print ("You lost. The word was ",word)
    print()

阅读3行# 我运行时得到的错误代码是:

  Traceback (most recent call last):
  File "C:\Users\Alex\Documents\codes\hangman.py", line 28, in <module>
    guessed.append(guess)
AttributeError: 'str' object has no attribute 'append'

正如我所说的那样没有#旁边的#代码但是我需要那些代码。有没有人有任何想法

1 个答案:

答案 0 :(得分:2)

您已将guessed转换为此行中的字符串:

guessed =" "   #put the space in there and reduce the length by one

如果您想要附加,请将其保留为列表。