python - 有人可以帮我用刽子手吗?

标签 python console

我正在使用 python 2.7.1 制作刽子手游戏。我试图让用户选择是否在游戏结束前重播。我正在尝试使用变量 keep_playing,但它不起作用。 另外,在 guesses=word_len * ['_'] 中,我想在下划线之间留一个空格,因为如果它们粘在一起,我就看不到还剩多少个字母。这是我的刽子手代码:

from random import *
import os


keep_playing = True
def print_game_rules(max_incorrect,word_len):
    print"you have only 7 chances to guess the right answer"
    return

def get_letter():
    print
    letter = raw_input("Guess a letter in the mystery word:") 
    letter.strip()
    letter.lower()
    print
    os.system('cls')
    return letter
def display_figure(hangman):
    graphics = [
    """
        +--------+
         |
         |
         |
         |
         |
         |
     ====================
    """,
    """
        +-------
        |      o
        |
        |
        |
        |
    ====================
    """,
    """
        +-------
        |      o
        |      +   
        |      |
        |
        |
    ======================
    """,
    """
        +-------
        |       o
        |    ---+
        |       |
        |    
        |  
        |   
    =====================
    """,
    """
        +-------
        |       o
        |    ---+---
        |       |
        |        
        |         
        |     
        |
    =====================
    """,
    """
        +-------
        |       o
        |    ---+---
        |       |
        |      / 
        |     /   
        |    /     
        |     
    =====================
    """,
    """
        +-------
        |       o
        |    ---+---
        |       |
        |      / \   
        |     /   \  
        |    /     \    
        |     
    =====================
    """]

    print graphics[hangman]
    return



animals=["horse","donkey","dinosaur","monkey","cat","aligator","butterfly","buffallo","dragon"]
word=choice(animals)
word_len=len(word)
guesses=word_len * ['_']
max_incorrect=6
alphabet="abcdefghijklmnopqrstuvxyz"
letters_tried=""
number_guesses=0
letters_correct=0
incorrect_guesses=0


print_game_rules(max_incorrect,word_len)
while (incorrect_guesses != max_incorrect) and (letters_correct != word_len):
    letter=get_letter()
    if len(letter)==1 and letter.isalpha():
        if letters_tried.find(letter) != -1:
            print "You already picked", letter
        else:
            letters_tried = letters_tried + letter
            first_index=word.find(letter)
            if  first_index == -1:
                incorrect_guesses= incorrect_guesses +1
                print "The",letter,"is not the mystery word."
            else:
                print"The",letter,"is in the mystery word."
                letters_correct=letters_correct+1
                for i in range(word_len):
                    if letter == word[i]:
                        guesses[i] = letter

    else:
        print "Please guess a single letter in the alphabet."
    display_figure(incorrect_guesses)

    print ''.join(guesses)
    print "Letters tried so far: ", letters_tried
    if incorrect_guesses == max_incorrect:
        print "Sorry, too many incorrect guesses. You are hanged."
        print "The word was",word
        keep_playing = False
    if letters_correct == word_len:
        print "You guessed all the letters in the word!"
        print "The word was",word
        keep_playing = False



while keep_playing == False:
    user = raw_input("\n\tShall we play another game? [y|n] ")
    again = "yes".startswith(user.strip().lower())
    if again:
        keep_playing = True
    if not again:
        break

raw_input ("\n\n\nPress enter to exit")

最佳答案

print ''.join(guesses) 可以变成 print ' '.join(guesses) 在字母之间留空格

您需要在 while keep_playing == False: 之前执行 keep_playing = False 并且主游戏循环应该位于从该循环调用的函数中 如果 keep_playing == True:

关于python - 有人可以帮我用刽子手吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5838986/

相关文章:

python - 我可以更改 python 首先查找模块的顺序吗?

python - 请求 session 中 cookielib CookieJar cookie 的类型错误

c# - 当文档建议我不能读取超过 254 个字符时,为什么我可以使用 Console.ReadLine 读取超过 254 个字符?

C# 列格式化

c - 如何在控制台应用程序中设置WM_KEYDOWN消息拦截器?

javascript - 打印任何 javascript 对象

python - Django:是否可以在缓存的 QuerySet 上执行查询而不是查询数据库?

python - 最好在 Python 中返回或更新对象属性?

python - Pandas 中几个数据帧的异步 'read_csv' - 为什么不更快

Java 控制台应用程序 : accepting commands from infinite loop