[Tutor] Need a solution.

David david at abbottdavid.com
Sat Jun 13 04:10:50 CEST 2009


Alan Gauld wrote:

> 
> This is a bad choice of class. It is a verb which is indicative
> that its not an object. Objects are nearly always nouns.
> 
> You could use GuessableNumber.
> Then it would have a number attribute which is the number
> to guess. It could have a generate() ,method which produces
> a new random number, possibly within a given range?
> 
> You could also add a compare method so you can compare
> your guesses. The compare method could count the number
> of calls and raise an exception if you compare more than N times.
> (The value of N would be in the constructor.
> Something like:
> 
> class GuessError(Exception): pass
> class GuessedNumber:
>     def __init__(self,tries=None,limits=None):...
>     def generate(self, limits=None):...
>     def __cmp__(self, aNumber):
>           if self.count >= self.tries: raise GuessError
>           ...
> 
> So I'd expect the client code to look like
> 
> target = GuessableNumber(tries=5, limits=(1,99))     # init calls 
> generate internally
> guess = int(raw_input('Guess-> '))
> while True:
>    try:
>        if guess == target:
>            print "Well done!"
>            break
>        else:
>            guess = int(raw_input('Guess again-> '))
>    except GuessError:
>           print "Sorry, too many guesses, the number was", target.number
> 
> 
> You could add another loop to ask if the player wanted to go again,
> in which case you call target.generate() directly to reset the target.
> 
> HTH,
> 

Hi Alan,
Thanks always for the feedback, i came up with this, not sure if it is 
much better but I am learning as I prod along :)

#!/usr/bin/python
from random import randrange
from sys import exit

class GuessedNumber:
     def __init__(self, attempts=None):
         self.attempts = attempts
         self.number = randrange(1,99)

class Counter:
     def __init__(self):
         self.value = 0
     def step(self):
         self.value += 1
     def current(self):
         return self.value

def play():
     c = Counter()
     guessit = GuessedNumber(attempts=5)
     target_number = guessit.number
     attempts = guessit.attempts
     guess = int(raw_input('Guess-> '))
     c.step()
     while c.current() < attempts:
         try:
             if guess == target_number:
                 print "Well Done"
                 play_again()
             elif guess < target_number:
                 print 'Higher ... '
                 guess = int(raw_input('Guess Again-> '))
                 c.step()
             elif guess > target_number:
                 print 'Lower ... '
                 guess = int(raw_input('Guess Again-> '))
                 c.step()

         except ValueError:
             print 'You must enter a number'
             pass

     print 'Too many attempts, the number was', target_number
     play_again()
def play_again():
     answer = raw_input('Do you want to try again? y/n ')
     answer = answer.lower()
     if answer == 'y':
         play()
     else:
         exit()


if __name__ == "__main__":
     play()
-- 
Powered by Gentoo GNU/Linux
http://linuxcrazy.com


More information about the Tutor mailing list