Home > OS >  Error in my Python guessing game. errors appearing
Error in my Python guessing game. errors appearing

Time:03-12

I made a Guessing game in python in which there will be a String That you will have to guess and input it. You get 3 tries and if failed a printed statement will show up saying "Game lost", But I am getting errors that I am unable to resolve. Here is the code:

Total_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")
 

CodePudding user response:

Please share your error(s) with us.

First of all there are simple syntax problems in your code. Indentation is very important concept in Python.

Indentation:

while something:
    doThis()

or something like this,

if something:
    doThat():

Also there are some variables you didn't define. If you don't define you can't use them. guess, Number_of_Guesses These are very important things if you are beginner.

Here is a fixed version of your code:

total_guess_count = 3
guess_count = 0
secret_word = "Lofi"

while guess_count < total_guess_count:
    
    guess = input("Enter your guess: ")
    
    if guess == secret_word:
        print("You win!")
        break
    else:
        guess_count  = 1
        
        if guess_count == total_guess_count:
            print("You lost!")
        else:
            print("Try again!")

You should obey the naming variable rules to get better. Such as, generally you don't start with uppercase characther. You defined "Guess" but tried to check for "guess" in while condition.

And you should start to check winning condition first, if you try to make it in else body, program wouldn't work healthy. I mean yes it can work but probably it would be buggy.

  • Related