Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Help - Guessing Game
#1
Hi all, you will have to excuse me, I am new to Python and have been learning over the past 2 days.

I have coded a basic guessing game and have a slight error.


First of all, I got the game to work perfectly as shown below;
secret_word = "spice"
guess = ""
guess_count = 0
guess_limit = 5
out_of_guesses = False

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

if out_of_guesses:
    print("Out of Guesses, YOU LOSE!")
else:
    print("YOU WIN!")
I then wanted to advance and interact more by saying "this is your first guess" which works perfectly.

I then tried to add in, "This is your last guess" which doesn't work in the correct way, it continues to offer me to answer, unless I guess correct then it will display "YOU WIN!"

Code shown below;
secret_word = "spice"
guess = ""
guess_count = 0
guess_limit = 5
out_of_guesses = False

guess = input("Enter your first guess: ")
while guess != secret_word and not (out_of_guesses):
    if guess_count < guess_limit:
        guess = input("Enter another guess: ")
        guess_count += 1
    elif guess != secret_word and not (out_of_guesses):
        guess = input("Enter your last attempt: ")
    else: out_of_guesses = True

if out_of_guesses:
    print("Out of Guesses, YOU LOSE!")
else:
    print("YOU WIN!")
Can anybody advise, I will be very grateful.

Cheers Guys!! Big Grin
Reply
#2
you could write it like this:
secret_word = "spice"
guess_count = 0
guess_limit = 5
guess = None

while guess_count < guess_limit:
    guess_count += 1
    guess = input(f"Enter guess number {guess_count}: ")
    if guess == secret_word:
        break

if guess_count == guess_limit:
    print("Out of Guesses, YOU LOSE!")
else:
    print("YOU WIN!")
try with correct answer:
Output:
Enter guess number 1: apple Enter guess number 2: pear Enter guess number 3: flour Enter guess number 4: spice YOU WIN!
try without finding answer:
Output:
Enter guess number 1: apple Enter guess number 2: pear Enter guess number 3: flour Enter guess number 4: ginger Enter guess number 5: nutmeg Out of Guesses, YOU LOSE!
Reply
#3
That is perfect. Thank you for you help.

Was there a way to manipulate my code? I am just trying to understand my mistakes.

Again, much appreciated.


Also, can I ask what the f in your code refers to?
Reply
#4
Your problem is here:
    elif guess != secret_word and not (out_of_guesses):
        guess = input("Enter your last attempt: ")
    else: out_of_guesses = True
There is no way you can reach "out_of_guesses = True.

To make this more obvious I am going to remove the guessing part of the game which should leave us a program that loops 5 times then exits.
guess_count = 0
guess_limit = 5
out_of_guesses = False
while not out_of_guesses:
    if guess_count < guess_limit:
        guess_count += 1
    elif not out_of_guesses:
        pass # Must have something after elif line
    else:
        out_of_guesses = True
"if guess_count < guess_limit:" is True for the first four guesses. On the fifth time through it is False, so we advance to "elif not out_of_guesses". not out_of_guesses is True, because we initialized out_of_guesses = False. On the sixth time through and every time after we execute the "elif" code and see that "not out_of_guesses" is True. As long as the elif is True, we cannot reach the final "else"
Reply
#5
All very helpful, really appreciate you help!

I did manage to get Python to do as I wanted it to, code shown below, probably very messy but hey ho!

secret_word = "spice"

print(" ")
print("Welcome to the 'Spice Guessing Game' you have 5 attempts. Good Luck!")
print(" ")

if secret_word == input("Enter your first guess: "):
    print(" ")
    print("You win Buddy!")
elif secret_word == input("Enter your second guess: "):
    print(" ")
    print("You win Buddy!")
elif secret_word == input("Enter your third guess: "):
    print(" ")
    print("You win Buddy!")
elif secret_word == input("Enter your fourth guess: "):
    print(" ")
    print("You win Buddy!")
elif secret_word == input("Enter your fifth and final guess: "):
    print(" ")
    print("You win Buddy!")
else:
    print(" ")
    print("You haven't guessed correctly, please come back and try again!")
Reply
#6
guesses = ['first', 'second', 'third', 'fourth', 'final']

for guess in guesses:
    if secret_word == input(f'Enter your {guess} guess: '):
        print('\nYou win Buddy!')
        break
else:
    print(f'The word was {secret_word}. Please come back and play again!')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Beginner Boolean question [Guessing game] TKB 4 2,340 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Unable to count the number of tries in guessing game. Frankduc 7 1,928 Mar-20-2022, 08:16 PM
Last Post: menator01
  Guessing game problem IcodeUser8 7 3,657 Jul-19-2020, 07:37 PM
Last Post: IcodeUser8
  Beginner Code, how to print something after a number of turns (guessing game) QTPi 4 2,760 Jun-18-2020, 04:59 PM
Last Post: QTPi
  Guessing game kramon19 1 2,185 Mar-25-2020, 04:17 AM
Last Post: deanhystad
  Help for guessing game code Kronos 5 3,301 Mar-09-2020, 04:53 PM
Last Post: snippsat
  guessing the number game go127a 6 4,862 Apr-27-2019, 01:23 PM
Last Post: go127a
  Guessing Game does not work the_entrepreneur 3 2,814 Apr-20-2019, 06:19 AM
Last Post: SheeppOSU
  Guessing Game with .WAV Files - Python Coding NonEntity 8 4,372 Nov-20-2018, 12:53 AM
Last Post: NonEntity
  Need help with simple guessing game! ghost0fkarma 2 2,818 Nov-03-2018, 01:19 AM
Last Post: ghost0fkarma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020