0

I am a beginner in Python and currently learning how to code in it. I made my first guessing game in Python.

(https://i.stack.imgur.com/ZWb8u.png)

Normally, I should be able to enter the guess. But I cannot enter any guesses in it. It's prompting me that I'm out of guesses. Please help.

Original Code

guess_limit = 3
out_of_guesses = True

while guess != secret_word and not (out_of_guesses):
    if guess_count < guess_limit:
        guess = input("Enter guess: ")
        guess_count += 1
    else:
        out_of_guesses = False

if out_of_guesses:
    print("Out Of Guesses, You Lose! ")
else:
    print("You win!")`enter code here`

Output

C:\Users\hites\PycharmProjects\Test\venv\Scripts\python.exe C:/Users/hites/PycharmProjects/Test/app.py Out Of Guesses, You Lose!

Process finished with exit code 0

hhsyhhsy
  • 1
  • 2

2 Answers2

1

Show the full code: we don't know what guess_count or secret_word is.

Without those, no one can really help you.

I made up guess_count and secret_word, to get the following (working) code:

guess_limit = 3
out_of_guesses = False

secret_word = 'kite' # put the secret word here
guess_count = 1

guess = input("Enter guess: ").lower()

while guess != secret_word and not (out_of_guesses):
    if guess_count < guess_limit:
        guess = input("Enter guess: ").lower()
        guess_count += 1
    else:
        out_of_guesses = True

if out_of_guesses:
    print("Out Of Guesses, You Lose! ")
else:
    print("You win!")

That was based on what I thought you were doing in the other lines of code (the part you haven't shown), though. So, I reconstructed your code after adding other lines. I used random.choice() for getting secret_word.

Here it is, completed:

from random import choice

guess_limit = 3
out_of_guesses = False

guess_count = 1

secret_word = choice(['password','kite','name','foul','wire','name','locker','computer','mud','wolf','fox','pliers','piano','chair','monk'])

guess = input("Enter guess: ").lower()

while guess != secret_word and not out_of_guesses:
    if guess_count < guess_limit:
        guess = input("Enter guess: ").lower()
        guess_count += 1
        out_of_guesses = False
    else:
        out_of_guesses = True

if out_of_guesses:
    print("Out Of Guesses, You Lose! ")
else:
    print("You win!")

Also, you had all your booleans mixed up, with weird parts in your code.

At this point, though, this answer is mostly based on guesswork about what you're trying to do.

Still, I hope this helps.

AashvikT
  • 164
  • 9
0

You could solve it this way:

guess_limit = 3
guess_count = 1
out_of_guesses = True
secret_word = "Secret Word"
guess = input("Enter guess: ")

while guess != secret_word and out_of_guesses:
    if guess_count < guess_limit:
        guess = input("Enter guess: ")
        guess_count += 1
    else:
        out_of_guesses = False

if not out_of_guesses:
    print("Out Of Guesses, You Lose! ")
else:
    print("You win!")

Result:

Enter guess: Attempt1
Enter guess: Attempt2
Enter guess: Attempt3
Out Of Guesses, You Lose!

Enter guess: Secret Word
You win!
Cow
  • 2,543
  • 4
  • 13
  • 25