################################# # Module: Bowling.py # Author: MobileDigit # Date: 2006-12-12 04:00 # Version: 1.0 """ Bowling is a class with a function called bowl that returns the values of the two bowls. It also adds the value to the total score of the object. The object's bowls are determined by a PRNG. If the object knocks all 10 pins on the first bowl (a strike), the object receives 20 points. If the object knocks all 10 using both bowls (a spare), the object receives 15 points. Else the number of points is how many pins are knocked down. """ from random import randint class Bowling: def __init__(self, nameOfBowler): self.name = nameOfBowler self.score = 0 def bowl(self): pins = 10 t1 = randint(1, 10) pins -= t1 t2 = randint(0, pins) if t1 == 10: t1 = 20; t2 = 0 elif t1 + t2 == 10: t1 = 15; t2 = 0 self.score += t1 + t2 return t1, t2 if __name__ == '__main__': owner = Bowling('Owner') ownee = Bowling('Ownee') for i in range(10): print 'Current Frame: %s' % i for person in [owner, ownee]: roll1, roll2 = person.bowl() print '%s scored: %s and %s' % (person.name, roll1, roll2) print 'Total Score: %s' % person.score print print 'The final scores are:' for person in [owner, ownee]: print '%s: %s' % (person.name, person.score) if owner.score == ownee.score: print "It's a tie!" else: points = abs(owner.score - ownee.score) winner = owner.name if owner.score >= ownee.score else ownee.name print '%s wins by %s points.' % (winner, points)