使用脚手架 Python 更新刽子手游戏

Updating the Hangman Game with Scaffold Python

提问人:AJHedges 提问时间:10/2/2023 最后编辑:AJHedges 更新时间:10/2/2023 访问量:35

问:

我目前正在考虑更新刽子手游戏,以显示脚手架图像,每次猜出的答案是错误的时都会出现一个新的身体部位。有人告诉我,显示脚手架的最佳方法是在 def 函数中使用“文档字符串”。

在我的课程的 helpfil 提示部分,它说当 wrond 答案的编号为零时,将显示“空”脚手架,当错误答案的数量等于 1 时,将显示添加头部的脚手架等。

因此,我使用脚手架的文档字符串创建了def功能,但是当猜测错误时,我似乎无法让它添加另一个身体部位。任何帮助将不胜感激。编码新手,所以要学很多东西。

import random

def displayScaffold(MAX_WRONG):
    stages = ( """
                    ----------
                    |/       |
                    |        o
                    |       /|/
                    |       / /
                    |
                    ---
               """,
               """
                    ----------
                    |/       |
                    |        o
                    |       /|/
                    |       / 
                    |
                    ---
               """,
               """
                    ----------
                    |/       |
                    |        o
                    |       /|/
                    |        
                    |
                    ---
               """,
               """
                    ----------
                    |/       |
                    |        o
                    |       /|
                    |        
                    |
                    ---
               """,
               """
                    ----------
                    |/       |
                    |        o
                    |        |
                    |        
                    |
                    ---
               """,
               """
                    ----------
                    |/       |
                    |        o
                    |        
                    |        
                    |
                    ---
               """,
               """
                    ----------
                    |/       |
                    |        
                    |        
                    |        
                    |
                    ---
               """
    )
    return stages[MAX_WRONG]

WORDS = (   "PYTHON", "ESTIMATE", "DIFFICULT", "ANSWER", "XYLOPHONE", \
            "UNNECESSARY", "ADEQUATE", "FEASIBLE", "CHARACTER", \
            "CONGRATULATIONS", "SEQUENCE", "POSITION", "PROGRAM" )
MAX_WRONG = 6;
word = random.choice(WORDS)
so_far = "-" * len(word)
wrong = 0
used = []
print("\nWelcome to the Hangman Game!")
print("\nYou have", MAX_WRONG, "wrong attempts to guess the word.")
print(displayScaffold(MAX_WRONG))

while wrong < MAX_WRONG and so_far != word:
    print("\n", so_far, "\n")
    guess=input("Enter a letter: ")
    guess=guess.upper()
    while guess in used:
        print("\nYou have already guessed this letter\a", guess)
        guess=input("Enter a letter: ")
        guess=guess.upper()
    used.append(guess)
    if guess in word:
        print("\nYes,", guess, "is in the word.")
        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.\a")
        wrong += 1
        left = MAX_WRONG - wrong
        print("\nYou have", left, "guesses left.")
if wrong == MAX_WRONG:
    print("\nYou've been hanged!\a")
else:
    print("\nYou guessed it!\a")
print("\nThe word was", word)



input("\nPress Enter to exit")
python-3.x python-2.7 用户定义函数

评论


答:

0赞 Alex Hall 10/2/2023 #1

您只在游戏开始时调用一次,而不是在用户猜错之后调用。displayScaffold

评论

0赞 AJHedges 10/2/2023
啊,所以我需要更多地调用函数,尤其是在他们猜错了之后?
0赞 AJHedges 10/2/2023
其他一切看起来都还好吗?