游戏刽子手代码理解(Python Programming for the Absolute Beginner, 3rd Edition,Chapter 5)

Game Hangman code understanding (Python Programming for the Absolute Beginner, 3rd Edition,Chapter 5)

这是我在我的游戏书中找到的代码 Hangman:

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

而且我完全不明白这最后一部分 请帮忙!

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 是 _ _ T _ _ _