What's new

Python Pa code po using python

BrigthWin

Forum Veteran
Elite
Joined
Jul 21, 2020
Posts
1,638
Reaction
1,768
Points
650
Create simple word guessing game in Python. In this game, you will choose a hidden word, and the player has to guess each letter of the hidden word. The player will lose a life for every wrong answer and win if they guess the entire word.
# Here are the rules and instructions for the game:
1. You need to choose a hidden word, and the player's task is to guess it letter by letter.
2. The player starts with a certain number of lives (e.g., 5 lives).
3. At the beginning of the game, print a message to inform the player how many lives they have and display an empty line for each letter in the hidden word (use underscores to represent the letters). For example, if the hidden word is "PYTHON," and the player has 5 lives, it should look like this: "You have 5 attempts to guess the word.
Word: _ _ _ _ _ _"
4. Prompt the player to guess a letter and read their input.
5. Check if the guessed letter is in the hidden word. If it is, reveal the letter in the appropriate position(s) of the word display. If not, subtract one life from the player and print a message that the guess was wrong.
6. Continue the game until the player either guesses the entire word or runs out of lives.
7. If the player guesses the entire word, print a message declaring them the winner.
8. If the player runs out of lives, print a message declaring them the loser.
# Constraints:
  • Use only loops and conditional statements (if, elif, else).
  • Do not use functions, word lists, or lists to store data.
  • The hidden word should be in all capital letters.
  • It is a must that the player guesses per letter of the hidden word.




Screenshot_20231101-130937.jpg
 

Attachments

Sure! I can help you with creating a word guessing game in Python. Here's an example code that you can use as a starting point:

Python:
hidden_word = "PYTHON"
lives = 5
guessed_word = "_" * len(hidden_word)

print(f"You have {lives} attempts to guess the word.")
print(f"Word: {guessed_word}")

while lives > 0 and "_" in guessed_word:
    guess = input("Guess a letter: ").upper()

    if guess in hidden_word:
        for i in range(len(hidden_word)):
            if hidden_word[i] == guess:
                guessed_word = guessed_word[:i] + guess + guessed_word[i+1:]
        print("Correct guess!")
    else:
        lives -= 1
        print("Wrong guess!")

    print(f"You have {lives} attempts left.")
    print(f"Word: {guessed_word}")

if "_" not in guessed_word:
    print("Congratulations! You guessed the word correctly!")
else:
    print("Game over! You ran out of lives.")
    print(f"The word was: {hidden_word}")

In this code, we start by defining the hidden_word and the number of lives the player has. We also create a string guessed_word that is initially filled with underscores to represent the letters of the hidden word that the player has not guessed yet.

We then enter a while loop that continues until either the player guesses the entire word or runs out of lives. Inside the loop, we prompt the player to guess a letter, check if it is in the hidden word, and update the guessed_word accordingly. If the guess is correct, we print a message and display the updated guessed_word, and if the guess is wrong, we subtract one life from the player and display the remaining lives.

After the loop, we check if the player has guessed the entire word by checking if there are any underscores left in the guessed_word. If not, we print a congratulatory message, and if there are still underscores, we print a message declaring the game over and reveal the hidden word.

I hope this helps you get started with your word guessing game! Let me know if you have any further questions.
 

Similar threads

Back
Top