2011-11-14 101 views
-7

我無法從我的代碼中獲取正確的輸出。我需要它一個一個地提供頭部,身體,手臂和腿部,但它不會。請檢查我的代碼。我遇到了我製作的Hangman程序的問題

import random 
max_wrong = len(HANGMAN) -1 
WORDS = ("Caleb","Owen","Ben","Adriane","Marley") 
word = random.choice(WORDS) 
so_far = "-" * len(word) 
wrong = 0 
used = [] 

while wrong < max_wrong and so_far != word: #The start of the sequence to make it loop 
    print(HANGMAN[wrong]) 
    print("you used the following letters: ",used) 
    print("So far the length of the word is ",so_far) 

    guess = raw_input("What letter do you think is in the word? ") 
    guess = guess.upper() 

    while guess in used: 
     print("Hey,wait a minute you have already guessed that word.") 
     guess = raw_input("What letter do you think is in the word? ") 
     guess = guess.upper() 

used.append(guess) 

if guess in used: 
    print('Yes',guess,"is in the word") 
    # new so_far 
    new = " " 
    for i in range(len(word)): 
     if guess == word[i]: 
      new += guess 

    else: 
    new += so_far[i] 
    max_wrong = len(HANGMAN) -1 

    so_far = new 

else: 
    print("Sorry ", guess, " is not in the word") 
    wrong += 1 

if wrong == max_wrong: 
    print(HANGMAN[wrong]) 
    print"Oops sorry you have been hanged!" 

else: 
    print("You guessed it the word was ",word) 

通過我已經創建的「圖形」元組,但堆棧溢出不會讓我告訴它,所以我離開它背後的方式。它不會提供這個機構,它一直在說這封信是在這個詞裏。 輸出是這樣,即使它是不是在這個詞

------ 
| | 
| 
| 
| 
| 
| 
| 
| 
---------- 

('you used the following letters: ', []) 
('So far the length of the word is ', '-----') 
What letter do you think is in the word? n 
('Yes', 'N', 'is in the word') 

------ 
| | 
| 
| 
| 
| 
| 
| 
| 
---------- 

('you used the following letters: ', ['N']) 
('So far the length of the word is ', ' -----') 
What letter do you think is in the word? k 
('Yes', 'K', 'is in the word') 

    ------ 
    | | 
    | 
    | 
    | 
    | 
    | 
    | 
    | 
    ---------- 

('you used the following letters: ', ['N', 'K']) 
('So far the length of the word is ', ' ----') 
What letter do you think is in the word? 
+0

如何給出一些關於它是什麼和沒有做的描述。 –

+0

您應該添加一個問題。以及輸出結果和預期結果的例子。 – Kapep

+0

您的代碼中的縮進是否完全如上所示? –

回答

1

嗯,首先,它看起來像你有一個未定義的變量:

max_wrong = len(HANGMAN) -1 

你的意思是

max_wrong = len(word) -1 

??在這種情況下,您必須在定義單詞後對其進行定義。例如,你有一個縮進的「其他」(不知道這是否只是通過你的副本&粘貼)。如果你正在嘗試使用

new = " " 

設立一個空字符串,並且您若循環,否則將覆蓋新的空字符串,每次外面每個字母他們猜中時你可能要添加它貫穿循環。

這是你的班級作業嗎?

+0

謝謝,不,我只是對編程感興趣。我想如果我使用了max_wrong,那麼每當我錯了時,我是如何做到這一點的。 – Kbob1998

+0

你是什麼意思「它會下降的元組」? – maneesha

0

開始:

if guess in used: 
    print('Yes',guess,"is in the word") 

你不想檢查,如果猜字?