[Python Wpg] Fw: python problem - help!!

Jim Crerar jscrerar at compuserve.com
Mon Sep 24 14:36:00 EDT 2007


----- Original Message ----- 
From: Jim Crerar 
To: Chris 
Sent: Monday, September 24, 2007 12:26 PM
Subject: Re: [Python Wpg] python problem - help!!


Hi Chris,
The following is my version of what you are looking for:
==========================================================================
# Hockey.py          Date: 2007/09/24          Author: James S. Crerar

# Two hockey teams play eight times during the regular season. Team A
# wins five times and Team B wins the other three times. They meet in
# the playoffs in a best of seven series. Write a program that estimates
# the chances of Team B winning the series.

# QUESTION:
# Are team B's chances better in a 1 game "series", a 3 game series,
# a 5 game series, or a 7 game series? What would team B's chances of
# winning a 51 game series be?

# ANSWER:
# It looks like the more games in a series the less likely that team B
# will win the series.  (If team A wins 5 out of 8 games during regular
# season).

from random import *

winA = 5.0/8.0
nsims = 1000
print '\n     CHANCE OF WINNING USING %d SIMULATIONS' % nsims
series = (1,3,5,7,51)
for n in series:
    totalA = 0
    totalB = 0
    nn = 0
    while nn < nsims:
        games = 0
        winsA = 0
        winsB = 0
        while games < n:
            if random() <= winA:
                winsA += 1
            else:
                winsB += 1
            games += 1
        if winsA > winsB:
            totalA += 1
        else:
            totalB += 1
        nn += 1
    print '\nFor %d game series:' % games
    chanceA = float(totalA)/nsims * 100
    chanceB = float(totalB)/nsims * 100
    print '     Team A = %5.2f%s     Team B = %5.2f%s' % (
                                chanceA, '\x25', chanceB, '\x25')
raw_input('\n\nCourtesy: James S. Crerar\nPress enter to exit..')
==========================================================================
Thanks,
Jim

----- Original Message ----- 
  From: Chris 
  To: winnipeg at python.org 
  Sent: Sunday, September 23, 2007 6:44 PM
  Subject: [Python Wpg] python problem - help!!


   

   

  I need your help!

   

  I have this problem that I can’t seem to figure out.  Can you help?  The problem is listed below, and below it is my code that I have so far.

   

  ------------------------------------------------------------------------------------------------

  Two hockey teams play eight times during the regular season. Team A wins five times and Team B wins the other three times. They meet in the playoffs in a best of seven series. Write a program that estimates the chances of Team B winning the series. A sample run of the program might look like this:

Are team B's chances better in a 1 game "series", a 3 game series, a 5 game series, or a 7 game series? What would team B's chances of winning a 51 game series be? 

  -----------------------------------------------------------------------------------------------

   

  import random

  SERIES = 1000

  game = 0

  series = 0

   

  while series < SERIES:

      

      teamA = random.randint (1,8)

      teamB = random.randint (1,8)

      teams = teamA + teamB

   

      if random.randint(1,8) >= 5:

          'Team A' = team

      else:

          'Team B' = team

   

          game = game + 1

   

      series = series + 1

   

  print 'In %2d simulated series %2d won %2d ' % (SERIES,team,game)

  print 'so I estimate there is a',(game / 10.00),'% chance they will win the series.'



  No virus found in this outgoing message.
  Checked by AVG Free Edition.
  Version: 7.5.487 / Virus Database: 269.13.25/1018 - Release Date: 9/19/2007 3:59 PM





  No virus found in this outgoing message.
  Checked by AVG Free Edition.
  Version: 7.5.487 / Virus Database: 269.13.25/1018 - Release Date: 9/19/2007 3:59 PM





------------------------------------------------------------------------------


  _______________________________________________
  Winnipeg mailing list
  Winnipeg at python.org
  http://mail.python.org/mailman/listinfo/winnipeg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/winnipeg/attachments/20070924/db25902b/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.gif
Type: image/gif
Size: 3680 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/winnipeg/attachments/20070924/db25902b/attachment.gif>


More information about the Winnipeg mailing list