class GuessGame:
def __init__(self, secret):
self._secret = secret
self._nguesses = 0
def guess(self, value):
if (value == self._secret):
print("Well done - you have guessed my secret")
else:
self._nguesses += 1
print("Try again...")
def nGuesses(self):
return self._nguesses
g = GuessGame("cat")
g.nGuesses() == 0
True
g.guess("dog")
Try again...
g.nGuesses() == 1
True
g.guess("horse")
Try again...
g.nGuesses() == 2
True
g.guess("cat")
Well done - you have guessed my secret
g.nGuesses() == 2
True
class GuessGame:
def __init__(self, secret, max_guesses=5):
self._secret = secret
self._nguesses = 0
self._max_guesses = max_guesses
def guess(self, value):
if (self.nGuesses() >= self.maxGuesses()):
print("Sorry, you have run out of guesses")
elif (value == self._secret):
print("Well done - you have guessed my secret")
else:
self._nguesses += 1
print("Try again...")
def nGuesses(self):
return self._nguesses
def maxGuesses(self):
return self._max_guesses
g = GuessGame("fish", 3)
g.maxGuesses() == 3
True
g.guess("cat")
Try again...
g.guess("dog")
Try again...
g.guess("horse")
Try again...
g.guess("gerbil")
Sorry, you have run out of guesses