0

このハングマン ゲームを Python で作成しています。問題は play_hangman 関数にあります。推測が発生するたびに、定数「推測」が 1 ずつ増加します。「%s 推測が残っています!」と出力したいです。ここで、グローバル定数 (MAX_GUESSES) からローカル定数 (推測) を引いて %s を取得します。しかし、それは機能していないようで、下にインクリメントする必要があるときに「残り 6 つの推測」を吐き続けます。助言がありますか?

   # CONSTANTS
MAX_GUESSES = 6

# GLOBAL VARIABLES 
secret_word = 'claptrap' 
letters_guessed = []

# From part 3b:
def word_guessed():
    '''
    Returns True if the player has successfully guessed the word,
    and False otherwise.
    '''
    global secret_word
    global letters_guessed
    for i in secret_word:
        if i in letters_guessed:
            continue
        else:
            return False        
    return True


def print_guessed():
    '''
    Prints out the characters you have guessed in the secret word so far
    '''
    global secret_word
    global letters_guessed
    blanklist = []
    for i in secret_word:
        if i in letters_guessed:
            blanklist.append(i)
        else:
            blanklist.append("-")
    blanklist_string = "".join(blanklist)
    return blanklist_string

def play_hangman():
    # Actually play the hangman game
    global secret_word
    global letters_guessed
    global MAX_GUESSES
    # Put the mistakes_made variable here, since you'll only use it in this function
    mistakes_made = 0
    guesses = 0
    guesses_left = MAX_GUESSES - guesses

    while word_guessed() is False:
        print print_guessed()
        print "%s guesses left!" % guesses_left
        letter_choice = raw_input("Choose a letter:")
        if letter_choice not in secret_word:
            mistakes_made += 1
            print "you have made %s mistakes" %mistakes_made
            print "sorry, that is not in the word"
        letters_guessed.append(letter_choice)
        guesses +=1
        print "you have made %s guesses" %guesses
        word_guessed()

    while word_guessed() is True:
        print "Congratulations! You win :)"
        break
4

2 に答える 2

1

ループguesses_left = MAX_GUESSES - guessesの内側に配置する必要があります。while word_guessed() is False:現在のように、guesses_left = MAX_GUESSES - guesseswhenを設定してから、ループ内でguesses == 0のみ変更します。guessesそれはguesses_left変わらずに残ります。

于 2013-03-19T21:10:43.947 に答える
1

を再計算しませんguesses_left。各反復の開始時に行う必要がありますguesses_left = MAX_GUESSES - guesses

于 2013-03-19T21:10:59.877 に答える