我正在尝试编写一个刽子手游戏,当玩家获得剩下一定数量的生命时会打印不同的单词,但我无法弄清楚如何做到这一点。我有其他一切预期这个概念。我已经尝试过elif语句,但除了这个之外,我不知道还有什么可以尝试:
lives_remaining = 14
guessed_letters = ''
def play():
word = pick_a_word()
while True:
guess = get_guess(word)
if first_try(guess, word):
print('Excellent!')
break
elif second_try(guess, word):
print('Great!')
break
elif third_try(guess, word):
print('Ok!')
break
elif fourth_try(guess, word):
print('Close one!')
break
elif lives_remaining == 0:
print('Nope!')
print('The word was: ' + (word))
break
def first_try(guess, word):
if lives_remaining > 12:
def second_try(guess, word):
if lives_remaining > 8:
def third_try(guess, word):
if lives_remaining > 4:
def fourth_try(guess, word):
if lives_remaining > 0:
def pick_a_word():
return random.choice(words)
def get_guess(word):
print_word_with_blanks(word)
print('Lives Remaining: ' + str(lives_remaining))
guess = raw_input(' Guess a letter or whole word?')
return guess
def print_word_with_blanks(word):
display_word = ''
for letter in word:
if guessed_letters.find(letter) > -1:
# letter found
display_word = display_word + letter
else:
# letter not found
display_word = display_word + '-'
print(display_word)
def process_guess(guess, word):
if len(guess) > 1:
return whole_word_guess(guess, word)
else:
return single_letter_guess(guess, word)
def whole_word_guess(guess, word):
global lives_remaining
if guess == word:
return True
else:
lives_remaining = lives_remaining - 1
return False
def single_letter_guess(guess, word):
global guessed_letters
global lives_remaining
if word.find(guess) == -1:
# letter guess was incorrect
lives_remaining = lives_remaining - 1
guessed_letters = guessed_letters + guess
if all_letters_guessed(word):
return True
return False
def all_letters_guessed(word):
for letter in word:
if guessed_letters.find(letter) == -1:
return False
return True
play()
我只是不知道如何让这些功能发挥作用。 任何意见将不胜感激:)
答案 0 :(得分:0)
if numberOfLives == 1:
print 'response1'
elif numberOfLives == 2:
print 'response2'
elif ...
你也可以use a dictionary。