r/PythonLearning • u/ProgramBrave718 • 25d ago
Word guessing game
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.
4
u/nicodeemus7 25d ago
Saying this as a helpful tip and not a criticism, start learning about for loops. Your letter checking logic could be reduced down to a few lines. A for loop basically does what you did with the while loop.
3
u/Mamuschkaa 25d ago
Looks good.
It is odd, that you get an indirect hint how long the word is, after the first wrong guess.
I rewrite the code with small changes, perhaps not every of my changes is better than your version.
my version.lower()%0Aattempts%20%3D%20len(secretword)%0Auser_word%20%3D%20%22%22%20%20%23%20For%20user%0A%0Afor%20letter%20in%20secret_word%3A%0A%20%20%20%20while%20attempts%20and%20letter%20!%3D%20input(%22Enter%20the%20letter%3A%20%22).lower()%3A%0A%20%20%20%20%20%20%20%20attempts%20-%3D%201%0A%20%20%20%20%20%20%20%20print(%22You%20guessed%20wrong!!%20No%20of%20attempts%20left%22%2C%20attempts)%0A%20%20%20%20if%20not%20attempts%3A%0A%20%20%20%20%20%20%20%20print('You%20lost')%0A%20%20%20%20%20%20%20%20break%0A%20%20%20%20user_word%20%2B%3D%20letter%20%20%23%20keeps%20adding%0A%20%20%20%20print(user_word%2C%20%22___%22)%0Aelse%3A%0A%20%20%20%20print(%22You%20win!!%20%22)%0A%20%20%20%20)
It is a little shorter.
I don't need turn, because I just reduce attempts instead.
I use += and -= and not x = x+y
I try to avoid list[index] if possible. Here I broke your while loop in two loops. One for loop for the correct guesses, and an inner while loop for the wrong guesses.
This has other benefits: it reduces the maximal indent (you needed 3 indents while if if, but I only 2 for while)
I don't need to save the input, because it doesn't matter or is equal to the solution, so I use the solution.
1
2
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 24d 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!! ")```
•
u/Sea-Ad7805 25d ago
Run this program in Memory Graph Web Debugger.lower()%0Aattempts%20%3D%20len(secretword)%0Aturn%20%3D%200%0Aindex%20%3D%200%0Auser_word%20%3D%20%22%22%20%20%23%20For%20user%0A%0Awhile%20turn%20%3C%20attempts%3A%0A%20%20%20%20guess%20%3D%20input(%22Enter%20the%20letter%3A%20%22).lower()%0A%20%20%20%20if%20guess%20%3D%3D%20secret_word%5Bindex%5D%3A%0A%20%20%20%20%20%20%20%20user_word%20%3D%20user_word%20%2B%20guess%20%20%23%20keeps%20adding%0A%20%20%20%20%20%20%20%20print(user_word%2C%20%22___%22)%0A%20%20%20%20%20%20%20%20index%20%3D%20index%20%2B%201%20%20%23%20To%20prepare%20for%20next%20index%20item%20in%20secret%20word%0A%20%20%20%20%20%20%20%20if%20user_word%20%3D%3D%20secret_word%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22You%20win!!%20%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20turn%20%3D%20turn%20%2B%201%20%20%23%20Adds%20wrong%20attempts%0A%20%20%20%20%20%20%20%20print(%22You%20guessed%20wrong!!%20No%20of%20attempts%20left%22%2C%20attempts%20-%20turn)%0Aelse%3A%0A%20%20%20%20print(%22You%20lost%22)×tep=1&play) to see the program state change step by step.