import random

def guess_word(): words = ['apple', 'banana', 'orange', 'grape', 'watermelon'] word = random.choice(words) chances = 3 while chances > 0: print('The word contains', len(word), 'letters.') guess = input('Guess a letter or the whole word: ') if guess == word: print('Congratulations! You guessed the word!') return elif guess in word: print('You guessed a letter! Keep going!') else: chances -= 1 print('Oops! That's not correct. You have', chances, 'chances left.') print('Sorry, you ran out of chances. The word was', word)

def play_again(): answer = input('Do you want to play again? (yes or no) ') if answer.lower() == 'yes': return True else: return False

while True: guess_word() if not play_again(): break print('Thanks for playing!')

Python3 猜字谜游戏代码:带三次机会和重玩选项

原文地址: https://www.cveoy.top/t/topic/oXxD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录