8. Your own while/else


#1

I wanted to experiment a little bit with this game and also send a message telling the user how many guesses they have left, I run into issues with a syntax error when I try to print guesses_left to the user, and even if I remove that it does print you lose if you run out of guesses and codecademy asks me if I put in an else statement.

from random import randint

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

guesses_left=3
while guesses_left > 0:
    guess=int(raw_input("Your guess: "))
    if guess == random_number:
        print "You win!"
        break
    elif guess != random_number:
        print "You've used one guess, you have" guesses_left "guesses left."
        guesses_left -= 1
    else:
        print "You lose."

this is the first experience with coding I've ever had, so any help is greatly appreciated thank you.


#2

You are concatenating the variable in your elif incorrectly.

See print "You've used one guess, you have" guessed_left "guesses left."

When you want to concatenate strings, which is what you're doing here, you need to use the + operator.

Like so: "This is a test " + "string"

With this information, how can you solve your problem? :smile:


#3

How would I concatenate a string between two walls of text?


#4

Like so:

variable = "some string!"
"This is a test string called \"" + variable + "\". Isn't it cool?"

(\" means a literal quotation mark in case you were wondering :wink:)


#5

I think I am misunderstanding something, sorry


#6

Ah, you can't modify the variable guesses_left while in the middle of that conditional definition.

You should modify it after you print the message.


#7

Thank you, for now I've just removed the elif statement to continue the course, but I will definitely revisit it later and try again.


#8

Could have been solved by:

if guess == random_number:
   print "You win!"
   break;
 elif guess != random_number:
   print "You've used one guess, you have \"" + guesses_left + "\" guesses left."
   guesses_left -= 1
   break;
 else:
   print "You lose."

#9

I think the code is missing a way to differentiate from #guess and last guess. Even if you lose, you still have to subtract a guess to end the loop.
For example:

if guess == random_number:
   print "You win!"
   break
elif guess != random_number and guesses_left > 1:
   print "You've used one guess, you have \"" + guesses_left + "\" guesses left."
   guesses_left -= 1
else:
   print "You lose."
   guesses_left -= 1

#10

You've got the right idea, though the final else shouldn't need a decrement line seeing as it will only be entered if guesses is already at zero.


#11

I've tried your code and this is the output I'm getting


#12

nevermind, I give up, I can't seem to get your way to work


#14

Let me know if this is what you were trying to code

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;
elif guess != random_number:
print "You have used one guess, you have \"" + str(guesses_left -1) + "\" guesses left."
guesses_left -= 1
#break;
else:
print "You lose."