2016-03-24 6 views
0

ハングマンを作っています。ここに私のコードは次のとおりです。私はそれが次の#でそのコードなしで正常に動作したとしてPython hangmanエラー

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' 

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行を読んでそれにはそのコードが必要です。誰もが任意のアイデア

+0

guessed = ""#そこにスペースを入れて長さを1つ減らします – clutton

+0

'if space == True 'の条件の下で、' guessed'変数を ' 'list'のように' append'メソッドを持たない 'str'型の値です。 – woozyking

答えて

2

を持っていないあなたは、この行の文字列にguessedを変換:あなたはそれに追加したい場合

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

がリストとしてそれを残します。

+0

私にそれを打つ!しかし、ええ、それはあなたの問題があるところです。 – Firearrow5235