我一直在Python中製作這個Hang子手遊戲。問題出在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