用python写一个猜谜游戏
时间: 2023-06-04 09:03:50 浏览: 95
好的,我可以为您提供一些代码。以下是一个简单的猜谜游戏,您可以在Python中使用:
```
import random
words = ['apple', 'banana', 'orange', 'pear']
answer = random.choice(words)
correct = answer
clue = answer[0] + answer[-1]
word_guess = ''
store_letter = ''
count = 5
print('Welcome to "Guess the Word" game!')
print('You have 5 attempts at guessing letters in a word.')
print('Let\'s begin!')
while count > 0:
print('------------------')
print('Clue: ', clue)
print('Number of attempts left: ', count)
print('Letters used: ', store_letter)
word_guess = input('Guess a word: ').lower()
if word_guess == correct:
print('Congratulations! You guessed the word!')
break
else:
count -= 1
store_letter += ' ' + word_guess
print('Sorry, {} is not the correct word. Please try again.'.format(word_guess))
if count == 0:
print('------------------')
print('Game Over. The correct word was', correct)
```
这个游戏会随机选择单词,并给出第一个和最后一个字母的提示。你的任务是猜测单词,在尝试次数用完之前猜对。在每次尝试之后,你会得到一些回馈,告诉你那个词不是正确的,并且你用过哪些字母。
希望这可以帮到您!
阅读全文