#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    11
    Rep Power
    0

    Using simulations to solve a problem


    Hi all,

    I have been working on this darn problem for a while now and I was wondering if someone could help me out. This is a simulation for a raquetball game. I am trying to get my program to keep track of the results for a best of n (with n as a user input) matches. Here is what I have so far. I would really appreciate any help.
    Attached Files
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Originally Posted by adam12
    Hi all,

    I have been working on this darn problem for a while now and I was wondering if someone could help me out. This is a simulation for a raquetball game. I am trying to get my program to keep track of the results for a best of n (with n as a user input) matches. Here is what I have so far. I would really appreciate any help.
    Some initial impressions:
    1/ Don't use mixedCase; it makes your code look like Java. Use under_scores - ref PEP8.
    2/ getInputs returns four items, but this line only unpacks three items:
    Code:
    probA, probB, n = getInputs()
    (and you never pass m in, so the assignment to games in sim_n_games() will raise a NameError)
    3/ Use augmented assignment
    Code:
    prob_a += 1
    4/ Consider making a class SimulatedGame, which handles everything for running one game. Note that you only need to keep track of the one value - Increment if A wins, Decrement if B wins:
    Code:
    class SimulatedGame(object):
        __init__(self, prob_a, prob_b):
           ...
        
        def play(self):
            ...
            if a_wins:
                return 1
            return -1
    
    a_wins =  sum([SimulatedGame().play() for i in range(n)])
    b_wins = n - a_wins
    5/ Don't format text manually [in print_intro()] - use the textwrap module instead.

    --OH.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    11
    Rep Power
    0
    Ok hydroxide thanks for the help I will try to make your suggested changes.

IMN logo majestic logo threadwatch logo seochat tools logo