Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble with Recursion in Python
    primarykey
    data
    text
    <p>Working with python 2.7.</p> <p>The following code allows me to input the winning percentage of two teams (WP_1 and WP_2) a number of wins (k) and determine given the two team's winning percentages, the probability that team one will have more wins at the end of the season (Playoff_Probability):</p> <pre><code>def PlayoffProb(WP_1, k, WP_2): TProb_2 = 0 p = float(WP_1)/1000 q = float(WP_2)/1000 n = 162.0 G = math.factorial(n)/(math.factorial(k)*math.factorial(n-k)) Prob = G*(p**k)*((1-p)**(n-k)) for c in range(0, k): G_2 = math.factorial(n)/(math.factorial(c)*math.factorial(n-c)) Prob_2 = G_2*(q**c)*(1-q)**(n-c) TProb_2 += Prob_2 Playoff_Probability = Prob*TProb_2 print Playoff_Probability print TProb_2 </code></pre> <p>But what would be a lot easier is if the function could be written recursively so that it would perform the same operation over every possible value of k and return the total probability of ending the season with more wins (which I believe should be given by the Playoff_Probability for each value run through the function of k, which I've tried to set equal to Total_Playoff_Probability).</p> <p>I've tried the following code, but I get a TypeError telling me that 'float' object is not callable at the return Total_Playoff_Probability step. I'm also not at all sure that I've set up the recursion appropriately.</p> <pre><code>def PlayoffProb2(WP_1, k, WP_2): TProb_2 = 0 Total_Playoff_Probability = 0 p = float(WP_1)/1000 q = float(WP_2)/1000 n = 162.0 G = math.factorial(n)/(math.factorial(k)*math.factorial(n-k)) Prob = G*(p**k)*((1-p)**(n-k)) for c in range(0, k): G_2 = math.factorial(n)/(math.factorial(c)*math.factorial(n-c)) Prob_2 = G_2*(q**c)*(1-q)**(n-c) TProb_2 += Prob_2 Playoff_Probability = Prob*TProb_2 Total_Playoff_Probability += Playoff_Probability if k == 162: return Total_Playoff_Probability else: return PlayoffProb2(WP_1, k+1, WP_2) </code></pre> <p>Any help would be greatly appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload