将图形添加到hangman python

时间:2013-10-24 22:44:02

标签: python graphics

Graphics=['''
------------
|         |''','''


------------
|         |         
|          O''','''


------------
|         | 
|          O 
|         / |''','''

------------
|         | 
|          O 
|         / | 
|          | ''','''


------------
|         |
|          O 
|         / |
|          |
|         / | 
|
|            ''']


print("Welcome to Hangman! Guess the mystery word with less than 6 mistakes!")

words= ['utopian','fairy','tree','monday','blue'] 

i=int(input("Please enter a number (0<=number<10) to choose the word in the list: "))

if(words[i]):
    print("The length of the word is: " , len(words[i]))

    guesses=0

    while guesses<6:
        guess=input("Please enter the letter you guess: ")

        guessed=''
        guessed = guessed+guess[0]


        if(guess in words[i]):
            print("The letter is in the word.")
            print(''.join(c if c in guessed else '_' for c in words[i]))


        else:
            print("The letter is not in the word.")
            guesses=guesses+1
            print("Letters matched so far:" ,''.join(c if c in guessed else '_' for c in words[i]))

        if guesses==6:
            print("Failure. The word was:" , words[i])

        else:
            print("You found the word!")

我的Hangman程序在Python中的最后一个问题。获取血腥图形。这对我来说是最具挑战性的部分,因为我没有通过Python的年轻经验处理ASCII艺术。我在哪里将这些图形放入程序中?在else语句下?

2 个答案:

答案 0 :(得分:0)

是的,你是对的。如果玩家猜错,应显示图形。这将在第一个else块中进行检查。因此,您必须放置代码以在该块中打印图形。

您是否需要有关显示图形的进一步帮助?

答案 1 :(得分:0)

不要将其视为ASCII艺术,而应将其视为列表Graphics中的元素。您需要使用索引来访问所需的图形。一种方法是跟踪失败的猜测,然后在每次失败的猜测时只做:

print(Graphics[failed_guess])

但是,您已经记录了猜测次数并在尝试失败时递增它们,所以我只是使用它:

print(Graphics[guesses])

最后,用大写字母命名列表并不常见。通常人们为类名保留大写字母,之后的原因可能会有所帮助。这就是其中之一,养成遵循惯例的习惯,以后你会很高兴。

相关问题