O'Reilly Forums: Chapter 1: Long Exercise Question - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Chapter 1: Long Exercise Question

#1 User is offline   glenn.hurley 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 07-February 13

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!")

0

#2 User is offline   #TM# 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 44
  • Joined: 10-December 12
  • 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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users