Viewing Paste 412
Formated Paste
# guess.py import random, string NUM_ALLOWED = 10 naughty_guesses = ('Jack Sucks', 'Jack Blows', 'Jack Stinks') quitopts = ('quit', 'q', 'bye', 'exit') class guessgame: def __init__(self, num): self.num = num self.attempts = 0 self.guesses = [] def makeguess(self): self.attempts+=1 num = raw_input('Enter a random number from 1 to %d. ' % (NUM_ALLOWED) + 'To Quit: ' + ' or ' . join(["'%s'" % v for v in quitopts]) + ': ') if string.lower(num) in quitopts: return if num in naughty_guesses: print '\nShut up meanie\n' if num.isdigit(): num = int(num) self.guesses.append(num) if num == int(self.num): print 'You did it! It took you %s attempts to get it' % (self.attempts) if self.attempts > NUM_ALLOWED: print 'The number of guesses means you are particularly stupid' return True print "Try Again, %s is NOT the number, you've made %s attempts to get it." % (num, self.attempts) return False def showguesses(self): return ', ' . join(['<%s>' % v for v in self.guesses if v not in naughty_guesses]) or 'None Made' # guessgame.py import guess, random game = guess.guessgame(random.randint(1, guess.NUM_ALLOWED)) # makeguess() takes an input and returns if its the number while game.makeguess() == False: pass print 'The number was: %s' % (game.num) print 'Guesses made: ' + game.showguesses()
