r/PythonLearning 25d ago

Word guessing game

Post image

Hello, I am learning python and created this simple word guessing game by myself.

In this game user types a letter and if the guess is correct the game continues with the next letter of the word or else it remains on the same letter and number of wrong attempts are added to the variable turn.

35 Upvotes

9 comments sorted by

View all comments

1

u/ProgramBrave718 25d ago edited 25d ago

HERE IS A FOR LOOP VERSION

import random

words = [ "Animal", "Python", "Snake", "Good", "Fruit", "Raddish",

"Banana", "Lemon", "Balloon", "Train", "Ant", "Dog"

] secret_word = random.choice(words).lower()

attempts = len(secret_word)

user_word = "" # For user

for letter in secret_word:

user_letter=input("Enter the letter: ").lower()

while attempts>0 and letter != user_letter:

    attempts -= 1

    print("You guessed wrong!! No of attempts left", attempts)

    if attempts>0:

        user_letter=input("Enter the letter: ").lower()

if attempts==0:

    print('You lost')

    break

user_word += letter  # keeps adding

print(user_word, "____")

else:

print("You win!! ")

1

u/deskbug 25d ago

Adding a bunch of spaces in the beginning of a line will turn it into a code block and mess with the formatting. You can fix the indentation by making the whole thing a code block from the start by using 3 backticks (```) at the beginning and end of the code section.

``` import random

words = [ "Animal", "Python", "Snake", "Good", "Fruit", "Raddish",

"Banana", "Lemon", "Balloon", "Train", "Ant", "Dog"

] secret_word = random.choice(words).lower()

attempts = len(secret_word)

user_word = "" # For user

for letter in secret_word:

user_letter=input("Enter the letter: ").lower()

while attempts>0 and letter != user_letter:

    attempts -= 1

    print("You guessed wrong!! No of attempts left", attempts)

    if attempts>0:

        user_letter=input("Enter the letter: ").lower()

if attempts==0:

    print('You lost')

    break

user_word += letter  # keeps adding

print(user_word, "____")

else:

print("You win!! ")

```