Exercise 8/19, works in my cmd, prints double "Your guess"


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-python/lessons/loops/exercises/your-own-while--else?action=lesson_resume&link_content_target=interstitial_undefined

<In what way does your code behave incorrectly? Include ALL error messages.>
It works, but for some reason it is red, where green flag must be.

<What do you expect to happen instead?>

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 = 0
  guess = int(raw_input("Enter a guess"))
  if guess == random_number:
    print "You win!"
    break
  else:
    guesses_left -= 1
    if guesses_left == 0:
      print "You lose."
      break

#2

the exercise wants you to use while/else, which is a slightly better design program. This way, we don’t have to check if guesses_left is zero each iteration, the else clause simple runs when loop condition becomes false


#3

Hm. OK, thanks, I’ll reconsider.


#4

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