编辑如果你被允许自由改变功能,那么就做
def print_guessed(letter):
global letters_guessed
letters_guessed.append(letter)
return ''.join(c if c in letters_guessed else '-'
for c in secret_word)
否则:这是一个相当棘手的问题,因为它甚至没有接近做正确的事情,但我想我已经想通了!
secret_word = "answer"
letters_guessed = []
letter = ''
def fix_print_guessed(func):
def wrapper(guess):
global letters_guessed
global letter
letter = guess
letters_guessed.append(letter)
return func()
return wrapper
@fix_print_guessed
def print_guessed():
global secret_word
global letters_guessed
word = []
for i in range (0, len(secret_word)):
if letter == secret_word[i]: word.append(letter)
else: word.append('-')
word_str = ''.join(word)
return word_str
found = len(secret_word) * '-'
while '-' in found:
guess = raw_input('enter a letter: ')
res = print_guessed(guess)
found = ''.join(c1 if c1 != '-' else c2
for c1, c2 in zip(found, res))
print found