-1

我正在尝试编写一个控制台刽子手游戏。如果我有一个问题,它会打印一个人被他的头上吊,然后如果我再次错过它会打印新图像以及前一个图像(参见下面的输出)。如何只显示一张文字图片?

这是我的代码:

import random
import urllib


animals = urllib.urlopen('http://davidbau.com/data/animals').read().split()
#animals = ('dog','cat','sheep','mouse','frog','worm','bird','lion','dear')
while True:

    print 'time to play hangman'
    print 'hint: the word is an animail'
    secret = random.choice(animals)
    guesses = 'aeiou'
    turns = 5

    while turns > 0:
        missed = 0
        for letter in secret:
            if letter in guesses:
                print letter,
            else:
                print '_',
                missed += 1

        print

        if missed == 0:
            print 'You win!'
            break

        guess = raw_input('guess a letter: ')
        guesses += guess

        if guess not in secret:
            turns -= 1
            print 'Nope.'
            print turns, 'more turns'
            if turns < 5: print """
-------
|   |
|   O
|
|
|
|
|
---------
"""
            if turns < 4: print """
-------
|   |
|   O
| \_|_/
|
|
|
|
---------
"""
            if turns < 3: print """
-------
|   |
|   O
| \_|_/
|   |
|
|
|
---------
"""
            if turns < 2: print """
-------
|   |
|   O
| \_|_/
|   |
|  / \
|
|
---------
"""
            if turns < 1: print """
-------
|   |
|   O
| \_|_/
|   |
|  / \
| d   b
|
---------
"""
            if turns == 0:
                print 'The answer is', secret
                break


    user = raw_input("\n\tShall we play another game? [y|n] ")
    again = "yes".startswith(user.strip().lower())
    if not again:
        raw_input ("\n\n\nPress enter to exit")
        break

这是输出(注意它是如何打印多次的):

================================ RESTART ================================
>>> 
>>> time to play hangman
hint: the word is an animail
_ _ a _ _ i _ _
guess a letter: p
Nope.
4 more turns

-------
|   |
|   O
|
|
|
|
|
---------

_ _ a _ _ i _ _
guess a letter: g
Nope.
3 more turns

-------
|   |
|   O
|
|
|
|
|
---------


-------
|   |
|   O
| \_|_/
|
|
|
|
---------

_ _ a _ _ i _ _
guess a letter: l
Nope.
2 more turns

-------
|   |
|   O
|
|
|
|
|
---------


-------
|   |
|   O
| \_|_/
|
|
|
|
---------


-------
|   |
|   O
| \_|_/
|   |
|
|
|
---------

_ _ a _ _ i _ _
guess a letter: m
Nope.
1 more turns

-------
|   |
|   O
|
|
|
|
|
---------


-------
|   |
|   O
| \_|_/
|
|
|
|
---------


-------
|   |
|   O
| \_|_/
|   |
|
|
|
---------


-------
|   |
|   O
| \_|_/
|   |
|  / |
|
---------

_ _ a _ _ i _ _
guess a letter: n
Nope.
0 more turns

-------
|   |
|   O
|
|
|
|
|
---------


-------
|   |
|   O
| \_|_/
|
|
|
|
---------


-------
|   |
|   O
| \_|_/
|   |
|
|
|
---------


-------
|   |
|   O
| \_|_/
|   |
|  / |
|
---------


-------
|   |
|   O
| \_|_/
|   |
|  / | d   b
|
---------

The answer is crayfish

    Shall we play another game? [y|n] 
4

1 回答 1

2

您遇到的问题来自于您的游戏状态检查逻辑,坦率地说,很糟糕。您应该使用if/elif子句而不是if/if子句。例如:

if turns < 5: print "..."
if turns < 4: print "..."
if turns < 3: print "..."
if turns < 2: print "..."
if turns < 1: print "..."

所有这些条件都适用于turns=0。取而代之的是,颠倒1之前出现的术语5,然后用ifthen子句构造它,elif这样一次只能有一个为真。此外,您可以使用==代替,<这也可以解决问题。

于 2013-07-01T17:34:37.277 回答