I made a guessing game in Python where the goal is to guess a secret word. The player has three attempts to input the correct word. If the player uses up all their tries and fails to guess it correctly, the game prints “OUT OF GUESSES.” If the player succeeds, it prints “you win.”
However, I’m running into some errors in the code that I can’t seem to resolve. Below is the code I wrote:
Here the code I wrote:
codeTotal_Guesses = 3
Guess = ""
G = 1
Seceret_Word = "Lofi"
Out_of_Guesses = False
while guess != Seceret_Word and not(Out_of_Guesses):
if G < Number_of_Guesses:
guess = input("Enter your guess: ")
G += 1
else:
Out_of_Guesses = True
if Out_of_Guesses:
print("OUT OF GUESSES")
else:
print("you win")
Issues I’ve Noticed in the Code:
- Variable Name Typos:
- In the
whileloop, I mistakenly usedguessinstead ofGuess(variable names are case-sensitive). Seceret_Wordis also misspelled (should beSecret_Word).
- In the
- Incorrect Variable Name Reference:
- The
if G < Number_of_Guessesline referencesNumber_of_Guesses, which doesn’t exist. It should beTotal_Guesses.
- The
- Syntax Error:
- There’s no colon (
:) at the end of theif G < Number_of_Guessesline, which causes a syntax error.
- There’s no colon (
- Initialization Problem:
- The
Guessvariable needs to be referenced consistently throughout the code, but it is not used correctly in thewhileloop due to the typo.
- The
Below is the corrected version of your guessing game:
Corrected Code:
codeTotal_Guesses = 3
Guess = ""
G = 1
Secret_Word = "Lofi"
Out_of_Guesses = False
while Guess != Secret_Word and not Out_of_Guesses:
if G < Total_Guesses:
Guess = input("Enter your guess: ")
G += 1
else:
Out_of_Guesses = True
if Out_of_Guesses:
print("OUT OF GUESSES")
else:
print("you win")
Explanation of the Fixes:
- Corrected Typos:
- Changed
Seceret_WordtoSecret_WordandguesstoGuessto maintain consistency.
- Changed
- Fixed Variable Reference:
- Changed
Number_of_GuessestoTotal_Guessesto use the correct variable name.
- Changed
- Added Colon:
- Added the missing colon at the end of the
if G < Total_Guesses:line.
- Added the missing colon at the end of the
- Consistent Variable Usage:
- Ensured that the same variable names (
GuessandSecret_Word) are used throughout the code.
- Ensured that the same variable names (
This corrected version should now run without errors. When the user guesses the word correctly, it prints “you win,” and if they exhaust all their guesses, it prints “OUT OF GUESSES.”
Final Thought:
By following naming conventions and structuring logical conditions effectively, you ensure that your code is not only functional but also easy to read and maintain. Debugging becomes much easier when your variables are named consistently, and your conditions are logically ordered. Always keep refining your code these small improvements will make a big difference in writing bug-free programs!

