[Tutor] Need a solution.
ALAN GAULD
alan.gauld at btinternet.com
Sat Jun 13 18:37:52 CEST 2009
> Ok, I think I am getting somewhere now :)
A lot better, now add the comparison methods:
> class GuessedNumber:
> def __init__(self, count=0, attempts=None):
> self.attempts = attempts
> self.number = randrange(1,99)
> self.count = count
> def step(self):
> self.count += 1
> def current(self):
> return self.count
def __lt__(self,other):
return self.number < other
def __gt__(self,other):
return self.number > other
def __eq__(self,other):
return self.numer == other
[caveat: There are better ways of doing this but this illustrates
the principle most clearly!]
And now you can write your tests as
> def play():
> guessit = GuessedNumber(attempts=5)
> guess = int(raw_input('Guess-> '))
> guessit.step()
> while guessit.current() < guessit.attempts:
> try:
if guess == guessit
etc...
HTH,
Alan G.
More information about the Tutor
mailing list