Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.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-需要为刽子手游戏显示图形_Python_Python 3.5 - Fatal编程技术网

Python-需要为刽子手游戏显示图形

Python-需要为刽子手游戏显示图形,python,python-3.5,Python,Python 3.5,我正在编写一个Python教程来编写一个Hangman游戏。我的电脑上安装了Python3.5,但本教程使用了Python2.7。除了显示图像外,游戏还可以运行。这是一个语法问题,还是有一种与我所学不同的方法来解决这个问题?这是我的密码 #!/usr/bin/env python 2 from random import * player_score = 0 computer_score = 0 # Define the graphics that will be used in the

我正在编写一个Python教程来编写一个Hangman游戏。我的电脑上安装了Python3.5,但本教程使用了Python2.7。除了显示图像外,游戏还可以运行。这是一个语法问题,还是有一种与我所学不同的方法来解决这个问题?这是我的密码

#!/usr/bin/env python 2

from random import *

player_score = 0
computer_score = 0

# Define the graphics that will be used in the game
def hangedman(hangman):
    graphic = [
        """
            +-------+
            |
            |
            | 
            |
            |
         ==============
        """
            ,
        """
            +-------+
            |       |
            |       0
            | 
            |
            |
         ==============
        """
            ,
        """
            +-------+
            |       |
            |       0
            |       |
            |
            |
         ==============
        """
            ,
        """
            +-------+
            |       |
            |       0
            |      -|
            |
            |
         ==============
        """
            ,
        """
            +-------+
            |       |
            |       0
            |      -|-
            |
            |
         ==============
        """
            ,
        """
            +-------+
            |       |
            |       0
            |      -|-
            |      /
            |
         ==============
        """
            ,
        """
            +-------+
            |       |
            |       0
            |      -|-
            |      / \
            |
         ==============
        """]

def start():
    print ("Let's play a game of Linux Hangman")
    while game():
        # pass will exit the loop if the player is done
        pass
    scores()

def game():
    # Create a set of words to play the game with
    dictionary = ["cat", "dog", "bird", "nest", "sun", "tree"]
    # Use the choice function from random mod to select a word
    word = choice(dictionary)
    word_length = len(word)
    # Create a clue with the number of underscores
    # equal to the word's length
    clue = word_length * ["_"]
    tries = 6
    letters_tried = ""
    guesses = 0
    letters_right = 0
    letters_wrong = 0
    global computer_score, player_score

    # Set up a loop that continues until player wins or loses
    while (letters_wrong != tries) and ("".join(clue) != word):
        letter = guess_letter()
        # Make sure entry is numeric and only 1 character long
        if len(letter) == 1 and letter.isalpha():
            if letters_tried.find(letter) != -1:
                print ("You've already picked", letter)
            else:
                letters_tried = letters_tried + letter
                # Search the word for the entered letter
                first_index = word.find(letter)
                # If letter is correct, let user know
                if first_index == -1:
                    letters_wrong += 1
                    print ("Sorry,",letter,"isn't what we're looking for.")
                else:
                    print ("Congratulations, ",letter," is correct.")
                    # Loop through the word and change the specific letter
                    # that is correct
                    for i in range(word_length):
                        if letter == word[i]:
                            clue[i] = letter
        else:
            print ("Choose another.")

        # Display the graphic
        hangedman(letters_wrong)
        # Print what the clue currently looks like
        print (" ".join(clue))
        print ("Guesses: ", letters_tried)

        # Check if the game is over
        if letters_wrong == tries:
            print ("Game Over")
            print ("The word was", word)
            computer_score += 1
            break
        if "".join(clue) == word:
            print ("You win!")
            print ("The word was", word)
            player_score += 1
            break
    return play_again()

# Get user input, sanitize, display it, and return it to be used
def guess_letter():
    letter = input("Take a guess at our mystery word: ")
    letter.strip()
    letter.lower()
    print (letter)
    return letter

# Ask to play again
def play_again():
    answer = input("Would you like to play again? y/n: ")
    if answer in ("y", "Y", "yes", "Yes", "Of course!"):
        return answer
    else:
        print ("Thank you very much for playing!")

def scores():
    global player_score, computer_score
    print ("HIGH SCORES")
    print ("Player: ", player_score)
    print ("Computer: ", computer_score)

# Used to execute in command line or import
# into another Python script. This will prevent
# the code from being executed when being imported.
if __name__ == '__main__':
    start()

似乎您的
hangedman
方法实际上没有打印任何内容或使用硬编码的图形列表。尝试在
hangedman
方法末尾的
graphic
列表下面添加这行代码:

print(graphic[hangman])

似乎您的
hangedman
方法实际上没有打印任何内容或使用硬编码的图形列表。尝试在
hangedman
方法末尾的
graphic
列表下面添加这行代码:

print(graphic[hangman])

嘿,你是如何执行这个脚本的?您在windows上吗?是的,我安装了适用于windows的Python 3.5,并且正在使用IDLE。
#如果播放机完成,pass将退出循环。
。否。
hangedman
不执行任何操作,也不返回任何内容。它在Python3中没有任何功能。它在Python2中什么也没做。你希望它做什么?嘿,你是如何执行这个脚本的?您在windows上吗?是的,我安装了适用于windows的Python 3.5,并且正在使用IDLE。
#如果播放机完成,pass将退出循环。
。否。
hangedman
不执行任何操作,也不返回任何内容。它在Python3中没有任何功能。它在Python2中什么也没做。你期望它做什么?