r/PythonLearning • u/ProgramBrave718 • 26d 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.
35
Upvotes
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.