Python3 猜字谜游戏代码 - 三次机会挑战
import random
def guess_word(word): chances = 3 while chances > 0: guess = input('请猜一下这个单词:') if guess == word: print('猜对了!') return True else: chances -= 1 if chances > 0: print('猜错了,请再接再厉!还剩{}次机会。'.format(chances)) print('很遗憾,机会用完了,正确答案是{}。'.format(word)) return False
def play_game(): words = ['apple', 'banana', 'cherry', 'durian', 'elderberry'] while True: word = random.choice(words) if guess_word(word): continue else: print('本轮游戏结束。') choice = input('是否要继续游戏?(y/n)') if choice == 'y': continue else: print('游戏结束。') break
play_game()
原文地址: https://www.cveoy.top/t/topic/oXQO 著作权归作者所有。请勿转载和采集!