8/19 'guess again' output too many times


#1

I am curious as to why my solution(which gives a correct result) is not accepted. The acceptable solution doesn't actually give you the correct output. Namely, you wind up being told to 'guess again' after your last guess. I had it set up so that it didn't output 'guess again' after your final guess, but that wasn't correct. I had:

while guesses_left > 0:
guess = raw_input("Your guess: ")
guess = int(guess)
if guess == random_number:
print "You win!"
break
elif guesses_left >=1:
print "Guess again!"
guesses_left -= 1
else:
print "You lose."
break

with appropriate tabs. Anyone have any insight?


#2
while guesses_left > 0:
    guess = int(raw_input("Your guess:"))
    if guess == random_number:
        print "You win!"
        break
    guesses_left -= 1
    if guesses_left > 1:
        print """Guesses left = %s.
Try again!""" %(guesses_left)
    elif guesses_left == 1:
        print """Guesses left = %s.
This is your last attempt!""" %(guesses_left)
else:
    print "You lose."

#3

I did it like this

from random import randint

# Generates a number from 1 through 10 inclusive
random_number = randint(1, 10)

print random_number # For debugging

guesses_left = 3 # Amount of guesses left
print "Guess my number! You've got 3 tries left!"

tries = [] # Array for user input

while guesses_left > 0:
    guess = int(raw_input("Your guess: ")) # Asks user to enter a number

    if guess in tries: # Checks if user entered the same number before
        print "You've already guessed this number: ", guess
        print "You've got " , int(guesses_left) , " guess(es) left!"

    elif guess > 10 or guess < 1: # Checks if user input is between 1 and 10
        print "Wrong input! Guess a number between 1 and 10!"
        print "You've got " , int(guesses_left) , " guess(es) left!"

    elif guess == random_number: # If user input is the same as the random number you win!
        print "You win!"
        break

    else:
        guesses_left -= 1 # Decreases the amount of guesses left
        print "You've got " , int(guesses_left) , " guess(es) left!"
        tries.append(guess) # Saves user input to "tries"
        
else:
    print "You lose!"

#4

This works for me.

from random import randint

# Generates a number from 1 through 10 inclusive
random_number = randint(1, 10)

guesses_left = 3
# Start your game!
while guesses_left > 0:
    guess = int(raw_input("Your guess: "))
    if guess == random_number:
        print 'You win!'
        break
    guesses_left -= 1
else:
    print 'You lose.'

#5

im stuck on this please help


#6

Here's the code I used. The only thing I didn't work into it was to reject inputs other than numbers and to reject numbers over 10.

from random import randint

# Generates a number from 1 through 10 inclusive
random_number = randint(1, 10)

guesses_left = 3
# Start your game!
print "Try to guess the correct number from 1 to 10!"
while guesses_left > 0:
    guesses_left -= 1
    guess = int(raw_input("Your guess: "))
    if guess == random_number:
        print "You WIN!"
        break
    
    else:
        print "Sorry, try again. Your remaining guesses = %s" % (guesses_left)
else:
    print "You lose"

Hope that helps you.


#7

The issue with this is (at least as far as I can tell), that this allowes me to make 4 user inputs (0-3) and not just 3. But if I change it to while guesses_left >1, then codecademy doesn't accept my solution anymore.

Could it be that this is a bug?


#8

guesses_left > 0 isn't 4 guesses though. >= 0 would be 4. If it's just > 0 the that means 3-1 because the 0 is not included.