Need help on Loops 8/19


#1

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-=guess
guess = int(raw_input("Your guess: ")
if guess == random_number
print "You win!"
break

else:
print "You lose."
It keeps saying that the "print "You win!"" is wrong but I don't know why


#2

When it gives you an error message like that I generally would just copy the text and paste it where your code it because it is anal-retentive about punctuation and capitalization.


#3
guess -= guess

I don't think this part is necessary since you're decrementing guess before asking for the user's input.
Look carefully at the instructions which tell you to decrement guesses_left by 1 after the break.

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."


The above code should work

#4

i did it like this, I've added some extra features just for fun:

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!"