Jump to content


Photo

Chapter 1: Long Exercise Question


  • Please log in to reply
1 reply to this topic

#1 glenn.hurley

glenn.hurley

    New Member

  • Members
  • Pip
  • 1 posts

Posted 07 February 2013 - 01:17 PM

On the long exercise solution in chapter 1, I got my code to work by putting the print("You win!") after the while loop and removed the if guess == 5 from the loop. Seems to work. Is there a reason I shouldn't do this?

from random import randint
secret = randint(1, 10)
guess = 0
print ("Welcome!")
while guess != secret:
  g = input("Guess the number: ")
  guess = int(g)
  if guess > secret:
      print ("Too high!")        
  else:
      if guess < secret:
        print ("Too low!")
print ("You win!")
print ("Game over!")


#2 #TM#

#TM#

    Active Member

  • Members
  • PipPipPip
  • 91 posts
  • Gender:Not Telling

Posted 08 February 2013 - 09:24 AM

THAT'S GOOD !! you have been able to compress your code by a different way of thinking.

There is no such reason for not doing that ; rather by doing it you will learn more tricks. That code is just there(in the book) to make us understand the basic things.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users