News:

Please note these forums are mostly a testing ground for my SMF work and I don't really use them otherwise.

Main Menu

Paste-1262393308:v:use_geshi-1:v:type-python

Started by akabugeyes, Jan 02, 2010, 12:48 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

akabugeyes

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'







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()

akabugeyes

# 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()