Viewing Paste 412

Formated Paste

  1. # guess.py
  2.  
  3. import random, string
  4.  
  5. NUM_ALLOWED = 10
  6. naughty_guesses = ('Jack Sucks', 'Jack Blows', 'Jack Stinks')
  7. quitopts = ('quit', 'q', 'bye', 'exit')
  8.  
  9. class guessgame:
  10. def __init__(self, num):
  11. self.num = num
  12. self.attempts = 0
  13. self.guesses = []
  14. def makeguess(self):
  15. self.attempts+=1
  16. num = raw_input('Enter a random number from 1 to %d. ' % (NUM_ALLOWED)
  17. + 'To Quit: ' + ' or ' . join(["'%s'" % v for v in quitopts]) + ': ')
  18. if string.lower(num) in quitopts: return
  19. if num in naughty_guesses: print '\nShut up meanie\n'
  20. if num.isdigit(): num = int(num)
  21. self.guesses.append(num)
  22. if num == int(self.num):
  23. print 'You did it! It took you %s attempts to get it' % (self.attempts)
  24. if self.attempts > NUM_ALLOWED: print 'The number of guesses means you are particularly stupid'
  25. return True
  26. print "Try Again, %s is NOT the number, you've made %s attempts to get it." % (num, self.attempts)
  27. return False
  28. def showguesses(self):
  29. return ', ' . join(['<%s>' % v for v in self.guesses if v not in naughty_guesses]) or 'None Made'
  30.  
  31. # guessgame.py
  32. import guess, random
  33.  
  34. game = guess.guessgame(random.randint(1, guess.NUM_ALLOWED))
  35. # makeguess() takes an input and returns if its the number
  36. while game.makeguess() == False:
  37. pass
  38. print 'The number was: %s' % (game.num)
  39. print 'Guesses made: ' + game.showguesses()
Name:
Email:
Code/text to paste:
  • Enable code highlighting
  • Code Language:
  • A duck, cat and a goose walk into a bar. How many animals walked into a bar?:
Highslide for Wordpress Plugin