Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
游戏刽子手代码理解(Python编程绝对初学者,第3版,第5章)_Python_Python 3.x - Fatal编程技术网

游戏刽子手代码理解(Python编程绝对初学者,第3版,第5章)

游戏刽子手代码理解(Python编程绝对初学者,第3版,第5章),python,python-3.x,Python,Python 3.x,这是我在书中找到的游戏代码。Hangman: HANGMAN=(""" ------ | | | | | | | | | ---------- """, """ ------ | | | O | | | | | | ---------- """, """ ------ | | | O | -+- | | | | | ---------- """, """ ------ | | | O | /-+- | | | | |

这是我在书中找到的游戏代码。
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.")
这部分代码必须创建一个新变量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
例如: word=PYTHON,guess='T',到目前为止是_

后for循环


到目前为止,我建议通过添加一些上下文,使您的答案更易于阅读。您可以在代码块中提供注释,或者将其格式化为围绕您要注释的代码的讨论。现在我明白了,但Paschalis Siskos您的注释中有一个错误:new+=到目前为止[I]#添加了已经猜测到new stringYes的‘‘’或字母,我关注的是第一个循环。这个循环的缩进有问题。
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