刽子手计划

2024-06-16 12:34:03 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我正在为我的CSCI课程做最后一个项目,我决定用一个刽子手程序,因为这是一个相当简单的游戏来实现。我的一切工作都很正常,除了我遇到的唯一问题是我的一些打印声明。代码如下:

import random
import os

HANGMANPICS = ['''

        +---+
        |   |
            |
            |
            |
            |
            |
      =========== ''','''
        +---+
        |   |
        O   |
            |
            |
            |
            |
      =========== ''','''
        +---+
        |   |
        O   |
        |   |
            |
            |
            |
      =========== ''','''
        +---+
        |   |
        O   |
       /|   |
            |
            |
            |
      =========== ''','''
        +---+
        |   |
        O   |
       /|\  |
            |
            |
            |
      =========== ''','''
        +---+
        |   |
        O   |
       /|\  |
       /    |
            |
            |
      =========== ''','''
        +---+
        |   |
        O   |
       /|\  |
       / \  |
            |
            |
      =========== ''']

ANIMALS = '''cat dog fish whale otter spider snake bird dolphin tiger mouse 
      rabbit bear lion'''
CITIES = '''indianapolis chicago orlando miami denver columbus memphis oakland
     seatlle phoenix dallas detroit baltimore cincinatti'''
COUNTRIES = '''india china america japan egypt greece mexico italy canada 
        australia france brazil germany korea'''

def Countries():
    print 'Guess the name of this country'
    return COUNTRIES

def Cities():
    print 'Guess the name of this city'
    return CITIES

def Animals():
    print 'Guess the name of this animal'
    return ANIMALS

def Random():
    print 'Guess the name of this randomly chosen country, city, or animal'
    return COUNTRIES + CITIES + ANIMALS

def Welcome():
    print ('Select a category:')
    print ('1: Countries')
    print ('2: Cities')
    print ('3: Animals')
    print ('4: Random')

    choice = {"1": Countries, "2": Cities, "3": Animals, "4": Random}
    choose = raw_input()
    return choice.get(choose, Random)().split()


def getRandomword(wordlist):
    wordindex = random.randint(0, len(wordlist)-1)
    return wordlist[wordindex]

def Display(HANGMANPICS,MISSEDLETTERS,CORRECTLETTERS,SECRETWORD):
    os.system('cls')
    print(HANGMANPICS[len(MISSEDLETTERS)])
    print

    print 'Missed letters:',
    for letter in MISSEDLETTERS:
        print letter,
    print

    blanks = '_' * len(SECRETWORD)

    for i in range(len(SECRETWORD)):
        if SECRETWORD[i] in CORRECTLETTERS:
            blanks = blanks[:i] + SECRETWORD[i] + blanks[i+1:]

    for letter in blanks:
        print letter
    print

def getGuess(alreadyguessed):
    while True:
        print 'Guess a letter'
        guess = raw_input()
        guess = guess.lower()
        if len(guess) != 1:
            print 'Please enter single letters'
        elif guess in alreadyguessed:
            print 'This letter has already been guessed, please guess again'
        elif guess not in 'abcdefghijklmnopqrstuvwxyz':
            print 'You did not guess a letter, please guess a letter'
        else:
            return guess

def playAgain():
    print ('Do you wanna play again? (Yes or No)')
    return raw_input().lower().startswith('y')

print ('Welcome to Hangman! By Aaron Taylor')
print (HANGMANPICS[6])
words = Welcome()
MISSEDLETTERS = ''
CORRECTLETTERS = ''
SECRETWORD = getRandomword(words)

done = False

while True:
    Display(HANGMANPICS,MISSEDLETTERS,CORRECTLETTERS,SECRETWORD)
    guess = getGuess(MISSEDLETTERS + CORRECTLETTERS)

    if guess in SECRETWORD:
        CORRECTLETTERS = CORRECTLETTERS + guess
        found = True
        for i in range(len(SECRETWORD)):
            if SECRETWORD[i] not in CORRECTLETTERS:
                found = False
                break

        if found:
             print('You won the game!')
             print('The correct word was ---->' + SECRETWORD.upper())
             done = True

    else:
        MISSEDLETTERS = MISSEDLETTERS + guess
        if len(MISSEDLETTERS) == len(HANGMANPICS)-1:
            Display(HANGMANPICS,MISSEDLETTERS,CORRECTLETTERS,SECRETWORD)
            print('You did not guess the word correctly. The word was : ' + SECRETWORD)
            done = True

    if done:
        if playAgain():
            os.system('cls')
            words = Welcome()
            MISSEDLETTERS = ''
            CORRECTLETTERS = ''
            done = False
            SECRETWORD = getRandomword(words)
        else:
            break

我希望得到打印声明在国家,城市,动物,并随机打印在实际的游戏屏幕后,作出了选择,但我没有任何运气,它的工作,如果我把操作系统('cls'),但它们有助于使程序看起来更好,这也是一个优点。太好了!谢谢!在


Tags: theinlenreturnifdefprintguess
3条回答

两种可能性:

1)像光标放置的libcurse。您可以将光标放在(文本)屏幕上的任何位置并打印出文本字符。我脑子里想不出比这更多的东西了——你得去查一下,希望python绑定到libcurse。(“urwid”在这里可能有用)

2)作弊的方式,可以使用,如果你只需要改变你打印的最后一行。在输出中使用\r(回车键),光标将移到行的开头并覆盖原来的内容。在

试着打电话系统stdout.flush()打印后的报表。在

问题是在Cities()、Countries()等中的print语句被调用之后调用os.system('cls')。让我们看看你的代码结构:

print ('Welcome to Hangman! By Aaron Taylor')
print (HANGMANPICS[6])
words = Welcome()
MISSEDLETTERS = ''
CORRECTLETTERS = ''
SECRETWORD = getRandomword(words)

done = False

while True:
    Display(HANGMANPICS,MISSEDLETTERS,CORRECTLETTERS,SECRETWORD)

Welcome()存在时,它调用适当的函数,比如Cities()

^{pr2}$

这个print语句被调用并打印出来,但几乎立即执行就会流入while True循环,该循环调用立即调用os.system('cls')Display(),删除{}内的打印。有道理吗?在

我认为最好的方法是将他们的选择存储在一个变量中,并在清除屏幕后让Display()打印正确的消息。但是,这将涉及到对代码的轻微重组。一种老套的方法是根据Welcome当前的返回值来获得他们的选择。以下是我对你的代码的破解,我认为它能满足你的需要:

def Display(HANGMANPICS,MISSEDLETTERS,CORRECTLETTERS,SECRETWORD, CHOICE):
    os.system('cls')
    CHOICE()
    print(HANGMANPICS[len(MISSEDLETTERS)])
    print

    print 'Missed letters:',
    for letter in MISSEDLETTERS:
        print letter,
    print

    blanks = '_' * len(SECRETWORD)

    for i in range(len(SECRETWORD)):
        if SECRETWORD[i] in CORRECTLETTERS:
            blanks = blanks[:i] + SECRETWORD[i] + blanks[i+1:]

    for letter in blanks:
        print letter
    print

def getGuess(alreadyguessed):
    while True:
        print 'Guess a letter'
        guess = raw_input()
        guess = guess.lower()
        if len(guess) != 1:
            print 'Please enter single letters'
        elif guess in alreadyguessed:
            print 'This letter has already been guessed, please guess again'
        elif guess not in 'abcdefghijklmnopqrstuvwxyz':
            print 'You did not guess a letter, please guess a letter'
        else:
            return guess

def playAgain():
    print ('Do you wanna play again? (Yes or No)')
    return raw_input().lower().startswith('y')

print ('Welcome to Hangman! By Aaron Taylor')
print (HANGMANPICS[6])
words = Welcome()
if words == ANIMALS:
    CHOICE = Animals
elif words == CITIES:
    CHOICE = Cities
elif words == COUNTRIES:
    CHOICE = Countries
else:
    CHOICE = Random

MISSEDLETTERS = ''
CORRECTLETTERS = ''
SECRETWORD = getRandomword(words)

done = False

while True:
    Display(HANGMANPICS,MISSEDLETTERS,CORRECTLETTERS,SECRETWORD,CHOICE)
    guess = getGuess(MISSEDLETTERS + CORRECTLETTERS)

相关问题 更多 >