Python Forum

Full Version: Guessing Game "limit 4 attempts" help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am on the last problem in my intro to information systems course. This problem is extra credit and it would be nice to get that extra boost on my grade.
I have most of the guessing game complete, but upon guessing the incorrect number, the program continues to allow the user to guess. I have referred to a variety of sources in order to help me out. What am I doing wrong in my code?

Quote:import random

winning_number = random.randint (1, 10)

guesses_remaining = 4

print("You have four chances to guess the number!\n")

keep_playing = "true"
while keep_playing == "true":

guess = int(input("Guess a number between 1 and 10: "))

guesses_remaining = guesses_remaining - 1
if guess == winning_number:
print("Nice! You guessed the correct number.")
keep_playing= "false"
elif guess != winning_number:
print("\nIncorrect, please try again.\n")
else:
if guesses_remaining == 0:
print("You ran out of tries! Better luck next time.")
keep_playing = "false"
The short answer is that branch never gets executed, because in your logic one of the two following branches always gets executed.
    if guess == winning_number:
        print("Nice! You guessed the correct number.")
        keep_playing= "false"
    elif guess != winning_number:
        print("\nIncorrect, please try again.\n")
Please read the Forum Rules and enclose your code in Python tags, not Quote tags in the future. Python tags preserve indentation which is very important.

There are several ways to make your code more Pythonic including:
a. while guesses_remaining > 0
b. guesses_remaining -= 1 #to subtract 1
c. Python has built in True and False (case sensitive)
d. Test for input being a number
e. Test for input between 1 and 10

Post your updated code if you are successful. If you need additional help, post your updated code, and Full Traceback errors and description of what problems you are having.

Lewis
Made some changes to my code after some input from others and this is what I ended up with.

import random

# Randomly choose winning number
winning_number = random.randint(1,10)

# Limits user to four guesses
guesses_remaining = 4

print("You have four attempts to guess the correct number. Give it a shot!")

keep_playing = "true"
while keep_playing == "true":   
    guess = int(input("\nGuess a number between 1 and 10: "))
    # Decrease guess counter by one each time to limit to four guesses.
    guesses_remaining = guesses_remaining - 1
    if guess == winning_number:
        print ("Winner! Winner! Chicken Dinner! You guessed the correct number. ")
        # End game after correct guess.
        keep_playing = "false"
    else:
    # Counter to determine amount of guesses left
        if guesses_remaining == 0:
            print ("\nYou are out of tries! The winning number was:",winning_number, "\n\nBetter luck next time!")
        # End game after max attempts.
            keep_playing = "false"
        elif guess != winning_number:
            print("\nSorry, that is not the correct number. Try again!")
If any discrepancies, I have not run into them yet. Thanks for the help.
Nice job for someone with limited exposure to Python. Take a look at a slightly modified version of your code for future reference to some common Python techniques:
import random
 
# Randomly choose winning number
winning_number = random.randint(1,10)
print("Debugging Aid: winning_number is '{}'".format(winning_number))  
 
# Limits user to four guesses
guesses_remaining = 4
 
print("You have four attempts to guess the correct number. Give it a shot!")
 

while  guesses_remaining > 0:

    while True:
        try:
            guess = int(input("\nGuess a number between 1 and 10: "))
            if 1 <= guess <= 10:
                break     #Exit 'while True' loop
            else:
                print("Your guess was NOT a number between 1 and 10.  Try again.")
        except:
            print("Your guess was NOT an Integer.  Try again.")
    

    # Decrease guess counter by one each time to limit to four guesses.
    guesses_remaining -= 1
    if guess == winning_number:
        print ("Winner! Winner! Chicken Dinner! You guessed the correct number. ")
        guesses_remaining = 0     # End game after correct guess.
    elif guesses_remaining == 0:
        print ("\nYou are out of tries! The winning number was:",winning_number, "\n\nBetter luck next time!")
    elif guesses_remaining == 1:
        print("\nSorry, that is not the correct number. Try again!  You have one try remaining.")
    else:
        print("\nSorry, that is not the correct number. Try again!  You have {} tries remaining.".format(guesses_remaining))
Lewis