用python写一个你画我猜游戏
时间: 2023-04-10 21:05:01 浏览: 174
好的,以下是一个简单的Python代码,实现了你画我猜游戏:
```python
import random
words = ["apple", "banana", "cherry", "orange", "pear"]
chosen_word = random.choice(words)
word_length = len(chosen_word)
print("Welcome to 'Guess the Word' game!")
print(f"The word has {word_length} letters.")
# Create a list of underscores to represent the letters in the chosen word
display = []
for _ in range(word_length):
display += "_"
# Loop until the player has guessed all the letters or run out of attempts
game_over = False
attempts = 6
while not game_over:
guess = input("Guess a letter: ").lower()
# Check if the letter has already been guessed
if guess in display:
print(f"You've already guessed {guess}.")
# Check if the letter is in the chosen word
for position in range(word_length):
letter = chosen_word[position]
if letter == guess:
display[position] = letter
# Check if the player has guessed all the letters
if "_" not in display:
game_over = True
print("You win!")
# Check if the player has run out of attempts
if guess not in chosen_word:
attempts -= 1
print(f"{guess} is not in the word. You have {attempts} attempts left.")
if attempts == 0:
game_over = True
print(f"You lose. The word was {chosen_word}.")
# Print the current state of the word
print(" ".join(display))
```
希望这个代码能够帮助你实现你画我猜游戏!
阅读全文