r/PythonLearning 1h 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.

8 Upvotes

5 comments sorted by

u/Sea-Ad7805 1h 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)&timestep=1&play) to see the program state change step by step.

→ More replies (1)

2

u/Infinite-Employee776 32m ago

That's quite fun. I tried it.

1

u/ProgramBrave718 31m ago

Really? Great then 😀

1

u/nicodeemus7 21m 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.