2015-01-09 68 views
-7

這是我在我的書中找到了比賽Hangman代碼:遊戲劊子手代碼的理解(對於絕對新手Python編程,第3版,第5章)

HANGMAN=(""" 
------ 
| | 
| 
| 
| 
| 
| 
| 
| 
---------- 
""", 
""" 
------ 
| | 
| O 
| 
| 
| 
| 
| 
| 
---------- 
""", 
""" 
------ 
| | 
| O 
| -+- 
| 
| 
| 
| 
| 
---------- 
""", 
""" 
------ 
| | 
| O 
| /-+- 
| 
| 
| 
| 
| 
---------- 
""", 
""" 
------ 
| | 
| O 
| /-+-/ 
| 
| 
| 
| 
| 
---------- 
""", 
""" 
------ 
| | 
| O 
| /-+-/ 
| | 
| 
| 
| 
| 
---------- 
""", 
""" 
------ 
| | 
| O 
| /-+-/ 
| | 
| | 
| | 
| | 
| 
---------- 
""", 
""" 
------ 
| | 
| O 
| /-+-/ 
| | 
| | 
| | | 
| | | 
| 
---------- 
""") 

MAX_WRONG = len(HANGMAN) - 1 
WORDS = ("OVERUSED", "CLAM", "GUAM", "TAFFETA", "PYTHON", "HARDWICKE", "BRADLEY", "SHEFFIELD") 

# initialize variables 
word = random.choice(WORDS) # the word to be guessed 
so_far = "-" * len(word)  # one dash for each letter in word to be guessed 
wrong = 0      # number of wrong guesses player has made 
used = []      # letters already guessed 


print("Welcome to Hangman. Good luck!") 

while wrong < MAX_WRONG and so_far != word: 
    print(HANGMAN[wrong]) 
    print("\nYou've used the following letters:\n", used) 
    print("\nSo far, the word is:\n", so_far) 

print("\nOnly use one letter values, more than one letter at a time does not work at this time") 
guess = input("\nEnter your guess: ") 
guess = guess.upper() 

while guess in used: 
    print("You've already guessed the letter", guess) 
    guess = input("Enter your guess: ") 
    guess = guess.upper() 

used.append(guess) 

if guess in word: 
    print("\nYes!", guess, "is in the word!") 

    # create a new so_far to include guess 
    new = "" 
    for i in range(len(word)): 
     if guess == word[i]: 
      new += guess 
     else: 
      new += so_far[i]    
    so_far = new 

else: 
    print("\nSorry,", guess, "isn't in the word.") 
    wrong += 1 

if wrong == MAX_WRONG: 
    print(HANGMAN[wrong]) 
    print("\nYou've been hanged!") 
else: 
    print("\nYou guessed it!") 

print("\nThe word was", word) 

input("\n\nPress the enter key to exit.") 

而且我不明白,這部分代碼:

if guess in word: 
    print("\nYes!", guess, "is in the word!") 

    # create a new so_far to include guess 
    new = "" 
    for i in range(len(word)): 
     if guess == word[i]: 
      new += guess 
     else: 
      new += so_far[i]    
    so_far = new 

else: 
    print("\nSorry,", guess, "isn't in the word.") 
    wrong += 1 

這部分代碼必須做出一個新的變量SOFAR,它會顯示正確的位置上與推測的字母有針對性字

# create a new so_far to include guess 
    new = "" 
    for i in range(len(word)): 
     if guess == word[i]: 
      new += guess 
     else: 
      new += so_far[i]    
    so_far = new 

而且我完全不懂這最後一部分 請幫忙!

+0

這個縮進有一個問題。 – njzk2 2015-01-09 20:13:41

回答

0
new = ""     #creates an empty string named new 
for i in range(len(word)): #loop x times,x is word size,e.g. len("PYTHON") is 6 
    if guess == word[i]: 
     new += guess  #adds the guess character to new string 
    else: 
     new += so_far[i] #adds the existing character from so_far string(on first loop is '_')   
so_far = new 

示例: 字= PYTHON,猜測= 'T',so_far爲_ _ _ _ _ _

後的for循環

so_far爲_ _Ť_ _ _

+0

我建議通過添加一些上下文來讓你的答案更容易閱讀。您可以通過在代碼塊中提供註釋來完成此任務,或者將其格式化爲圍繞您所評論的代碼的討論。 – dho 2015-01-09 19:42:11

+0

現在我明白了,但是Paschalis Siskos在評論中有錯誤:new + = so_far [i]#添加已經被猜到新字符串的'_'或字母 – 2015-01-10 11:58:30

+0

確實,我專注於第一個循環。 – 2015-01-10 12:50:05