CodeAcademy does not accept this as a valid answer. 8/19


#1

Hi CodeAcademy,

My python code works as expected with all the functionality required however CodeAcademy does not accept it as a valid answer.

My solution:

# 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: "))
   guesses_left -= 1
   if random_number == guess:
      print "You win!"
      break
   else:
      print "You lose."

CodeAcademy’s solution:

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

#2

no? You inform the user they lost after each incorrect guess, you only loose after running out of guesses, not after the first faulty guess


#3

Right got it thanks :slight_smile:


#4

This is why we use while/else, now the else clause will only run when the loop condition become false. If the loop breaks (user guesses correctly), the else clause won’t run


#5

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