8. Your own while / else


#1



Exercise
https://www.codecademy.com/courses/python-beginner-en-cxMGf/0/8?curriculum_id=4f89dab3d788890003000096#


I've tried this a few different ways and I'm stuck.

  1. If I run this and guess the correct number the code ends giving me a "congratulations" dialog even though the code is wrong.
  2. If I guess the wrong number it ends as if I've put a break in it, again with a "congratulations" dialog.
  3. I can't get it to decrease the guess count. I've ran it in the labs space to check these. It is not helpful that the code is passed as correct when it isn't. This isn't the first time I've had this.

I've got a few commented lines in the code so I could make sure I entered the wrong number and so I could see the remaining guesses. I also put the break in since it kept looping and freezing my browser. Something else that bothers me is I don't know if I'm supposed to get 3 guesses in this exercise or not. This stuff needs to be more clear :confused:


from random import randint

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

guesses_left = 3
print random_number
# Start your game!
guess = int(raw_input("Pick a number from 1 - 10:"))
while guesses_left > 0:
    if guess == random_number:
        print "You win!"
        break
    else:
        #print "Guesses remaining " + str(guesses_left)
        guesses_left -= 1
        guess
        #break
else:
    print "You lose."


#2

This line is not doing anything. Best remove.

If you put the guess input inside the while loop, before the if it will work.


#3

Oops. Yes that was my attempt to force it to go back through the loop. Even before the "If" statement it does nothing and has no impact on the rest of the failing code. Removed and updated code that still has the same issues:

from random import randint

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

guesses_left = 3
print random_number
# Start your game!
guess = int(raw_input("Pick a number from 1 - 10:"))
while guesses_left > 0:
    if guess == random_number:
        print "You win!"
        break
    else:
        #print "Guesses remaining " + str(guesses_left)
        guesses_left -= 1
        #break
else:
    print "You lose."

#4

Put the input inside the loop, not before it. Otherwise you can never get back to it.


#5

Thanks mtf! Things like that will (hopefully) become obvious the more I do this but until you pointed it out my sleep deprived brain couldn't figure it out. Slightly embelished code that now works (but won't work for the exercise due to my additions) below.

One question I have that you may be able to answer. I used this.

print "Guesses remaining: " + str(guesses_left)

I wanted it to display the remaining guesses and it does that. However, it felt like the wrong way or at least bad practice. Is this method ok or would there be a better way? Thanks again for the help and explanation.

from random import randint

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

guesses_left = 3
print random_number
# Start your game!
#guess = int(raw_input("Pick a number from 1 - 10:"))
while guesses_left > 0:
    print "Guesses remaining: " + str(guesses_left)
    guess = int(raw_input("Pick a number from 1 - 10:"))
    if guess == random_number:
        print "You win!"
        break
    else:
        guesses_left -= 1
        
else:
    print "No guesses remaining."
    print "You lose."

#6

There is nothing inherently wrong with this, but we can improve it using string formatting, rather than concatenation and string casting.

print "Guesses remaining: %d" % guesses_left

In Python 3, print is a function, so written as, print(), and a new format method has been added:

print ("Guesses remaining: {:d}".format(guesses_left))

#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.