Skip to content Skip to sidebar Skip to footer

Bug With A Program For A Guessing Game

I am creating a program where a user had to guess a random number in a specific range and has 3 tries to do so. After each try, if the guess is correct you tell the user they won a

Solution 1:

The simplest way to stop a loop when some condition becomes true is to use break. So when the player guesses correctly, the code would look like

if inp == x:
    guess = True
    print "Correct! You won"
    break

Additionally, you should break the loop when you run out of guesses, otherwise it will keep running

else:
    print "Wrong guess! You are out of tries"
    print "Game Over!"
    break

To let the player play again, you can have a variable playing and use it in a while loop that wraps the whole game, and when they say they don't want to play anymore, make it False so that the outer loop ends.

playing = True
while playing:
    x = round(random.random()*5)
    guess = False
    tries = 0
    while guess != True:
        ...

    again = raw_input("Do you want to play again? ")
    if again == "no":
        print "Okay thanks for playing!"
        playing = False
    elif again =="yes":
        print "Great!"

As you noted in comments on another answer, when the player gets their third guess wrong, the game tells them they have 0 guesses left and to try again, and then tells them they are out of tries and that the game is over. Since you want it to only tell them they are out of tries and that the game is over, you can change the main block of code to:

while guess != True:
    inp = input("Guess the number: ")
    if inp == x:
        guess = True
        print "Correct! You won"
    else:
        tries = tries + 1
        if tries == 3:
            print "Wrong guess! You are out of tries"
            print "Game Over!"
            break
        else:
            print "Wrong guess! Try again."
            print "Your remaining tries are",(3-tries)

Solution 2:

I guess this is it. Tested this out. Working Perfect (Y)

import random
x =round(random.random()*5)
guess = False
tries = 0
while guess != True:
    if tries<=3:
        inp = input("Guess the number: ")
        if tries<3:
            if inp == x:
                guess = True
                print "Correct! You won"
            else:
                print "Wrong guess! Try again."
                tries = tries + 1
                print "Your remaining tries are",(3-tries)
        if tries>2:
            if inp == x:
                guess = True
                print "Correct! You won"
            else:
                print "Wrong guess! You are out of tries"
                print "Game Over!"
                again = raw_input("Do you want to play again? ")
                if again == "no":
                    guess = True
                    print "Okay thanks for playing!"
                elif again =="yes":
                    tries = 0
                    print "Great!"

Hope this helps.

Here's more compact code you can try. Also it is solving your "Wrong Guess. Try Again." issue on last guess.

import random
x =round(random.random()*5)
guess = False
tries = 0
print x
while guess != True:
    while tries<3:
        inp = input("Guess the number: ")
        if inp == x:
            guess = True
            print "Correct! You won"
            break
        else:
            tries = tries + 1
            if tries == 3:
                break
            print "Wrong guess! Try again."
            print "Your remaining tries are", (3 - tries)

        if not guess:
            print "Wrong guess! You are out of tries"
            print "Game Over!"
        again = raw_input("Do you want to play again? ")
        if again == "no":
            guess = True
            print "Okay thanks for playing!"
        elif again == "yes":
            tries = 0
            if guess:
                guess = False
                x = round(random.random() * 5)
            print "Great!"

Solution 3:

this is a slightly different version to your problem:

import random

def play():
    x = round(random.random()*5)
    guess = False
    tries = 0
    while guess is False and tries < 3:
        inp = raw_input("Guess the number: ")
        if int(inp) == x:
            guess = True
            print "Correct! You won" 
        else:
            print "Wrong guess! Try again." 
            tries = tries + 1
            print "Your remaining tries are",(3-tries)
            if tries == 3:
                print("Game Over!")

play()
again = "maybe"
while again != "no":
    again = raw_input("Do you want to play again? yes OR no? \n")
    if again == "yes":
        print "Great!"
        play()
print "Okay thanks for playing!"

Solution 4:

You need to move where you set guess = True

import random
x =round(random.random()*5)
guess = False
tries = 0
while guess != True:
    if tries<=3:
        inp = input("Guess the number: ")
        if tries<3:
            if inp == x:
                guess = True
                print "Correct! You won"
            else:
                print "Wrong guess! Try again."
                tries = tries + 1
                print "Your remaining tries are",(3-tries)
        if tries>2:
            guess = True
            if inp == x:   
                print "Correct! You won"
            else:
                print "Wrong guess! You are out of tries"
                print "Game Over!"
again = raw_input("Do you want to play again? ")
if again == "no":
    print "Okay thanks for playing!"
elif again =="yes":
    print "Great!"

Before you were only ending on the condition where the last guess was correct. You should be ending whether the guess was correct or not.


Solution 5:

You need to make guess equal to True in your second else or else it will never satisfy stop condition for while loop. Do you understand?

import random
x =round(random.random()*5)
guess = False
tries = 0
while guess != True:
    if tries<=3:
        inp = input("Guess the number: ")
        if tries<3:
            if inp == x:
                guess = True
                print "Correct! You won"
            elif tries+1 != 3:
                print "Wrong guess! Try again."
                print "Your remaining tries are",(3-(tries+1))
            tries = tries + 1    
        if tries>2:
            if inp == x:
                guess = True
                print "Correct! You won"
            else:
                print "Wrong guess! You are out of tries"
                print "Game Over!"
                guess= True

                again = raw_input("Do you want to play again? ")
                if again == "no":
                    print "Okay thanks for playing!"
                elif again =="yes":
                    print "Great!"
                    guess = False
                    tries = 0

EDIT: I think this does the trick using your code.


Post a Comment for "Bug With A Program For A Guessing Game"