How to create "Guess the word" game in Python

Here's Python code for a word guessing game where the goal is to get the highest score:



import random

# list of words to choose from
words = ["apple", "banana", "grape", "orange", "mango", "pear", "mulberry", "guava", "papaya", "mandarin", "satsuma", "pineapple", "raspberry", "watermelon", "plum", "sugarcane", "soursop", "longan"]

print('''***Welcome to the "Guess the word" game***''')

print('''Fruits:( "apple", "banana", "grape", "orange", "mango", "pear", "mulberry", "guava", "papaya", "mandarin", "satsuma", "pineapple", "raspberry", "watermelon", "plum", "sugarcane", "soursop", "longan")''')
# initialize score to 0
score = 0

# play the game for 10 rounds
for i in range(10):
  # choose a random word from the list
  word = random.choice(words)
  # ask the user to guess the word
  guess = input(f"Guess the fruit name: {word[0]}****=")
  # if the guess is correct, add 1 to the score
  if guess == word:
    score += 1
  # print the current score
  print(f"Current score: {score}")

# print the final score
print(f"Final score: {score}")

This code will choose a random word from the list and ask the user to guess it. The user gets one point for each correct guess. The game is played for 10 rounds, and the final score is printed at the end.

Post a Comment