刽子手游戏 Python 错误

Posted

技术标签:

【中文标题】刽子手游戏 Python 错误【英文标题】:Hangman Game Python Error 【发布时间】:2014-10-19 10:35:08 【问题描述】:

我正在尝试在 python 中创建刽子手游戏,但出现以下错误 当我尝试运行该文件时。

guess = input("\n输入你的猜测:")

文件字符串”,第 1 行,在模块中

如果您知道解决此错误的方法,请告诉我

#!/usr/bin/python
# imports
import random

# constants
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.")

【问题讨论】:

出现错误时,调试它。 如果您需要帮助,至少提供minimal example(首先,没有人需要滚动浏览 100 行 ASCII 艺术)和完整的错误回溯。 这段代码在python3中运行良好。 【参考方案1】:
  guess = raw_input("\nEnter your guess: ")

应该是 raw_input,而不是 input。

【讨论】:

另外 \n 需要在引号中。如'\n'。 不,该代码看起来像 python 3 并且在 python 3 中输入是正确的。 这是 python3 代码,但将其转换为 python 2.x 所需的唯一转换是这里建议的,但请确保将 input() 的所有实例更改为 raw_input(),如果你'希望它在 python 2 下运行。 感谢您的评论。一旦我将所有输入更改为 raw_input,它现在在 python 2.7 中运行良好。

以上是关于刽子手游戏 Python 错误的主要内容,如果未能解决你的问题,请参考以下文章

UVa 489,紫书P79,刽子手游戏

例题4-1-3-古老的密码刽子手的游戏,救济金发放

初学者的python刽子手代码

试图制作make子手游戏,但陷入循环?

C getchar() 刽子手游戏

刽子手游戏