Python Forum
Guessing Game "limit 4 attempts" help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Guessing Game "limit 4 attempts" help
#1
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"
Reply
#2
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
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#3
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.
likes this post
Reply
#4
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
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Play again, in a guessing game banidjamali 4 11,705 Jan-22-2021, 06:23 AM
Last Post: banidjamali
  Help - random number/letter guessing game juin22 1 3,201 Aug-16-2020, 06:36 AM
Last Post: DPaul
  making a guessing number game blacklight 1 2,198 Jul-02-2020, 12:21 AM
Last Post: GOTO10
  Guessing game with 4 different digits LinseyLogi 6 3,648 Mar-29-2020, 10:49 AM
Last Post: pyzyx3qwerty
  Guessing game with comparison operators Drone4four 9 13,783 Dec-02-2018, 06:12 PM
Last Post: ichabod801
  Looping over amount of attempts Beatenberg 4 6,257 Oct-17-2017, 07:47 AM
Last Post: Beatenberg
  Name guessing game in python Liquid_Ocelot 6 15,015 Apr-01-2017, 12:52 PM
Last Post: ichabod801
  guessing script simon 3 5,568 Oct-10-2016, 01:47 PM
Last Post: simon
  trying to get my random number guessing game to work! NEED HELP RaZrInSaNiTy 4 6,871 Oct-06-2016, 12:49 AM
Last Post: tinabina22

Forum Jump:

User Panel Messages

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