Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Solved

Posted on 2013-01-10

Hi

Can anybody tell me why the following code won't work? I get a syntax error on

plt.figure()

This is the code:

import matplotlib.pyplot as plt

import math

def plotBinomial():

prob = input( 'Enter probability of success: ')

n = input( 'Enter the number of successes: ')

Nmax = input( 'Enter the maximum number of trials: ')

x = []

y = []

for N in range( n, Nmax + 1 ):

x.append( N )

y.append( probability( prob, n, N )

plt.figure()

plt.title( 'Varying N and probability p = ' + str( prob ) )

plt.xlabel( 'N' )

plt.ylabel( 'Probability' )

plt.grid( True )

plt.plot( x, y )

plt.show()

plt.close()

def probability( prob, n, N ):

nCr = math.factorial( N ) / ( math.factorial( n ) * math.factorial( N - n ) )

P = nCr * ( prob**n ) * ( 1 - prob )**( N - n )

return P

Can anybody tell me why the following code won't work? I get a syntax error on

plt.figure()

This is the code:

import matplotlib.pyplot as plt

import math

def plotBinomial():

prob = input( 'Enter probability of success: ')

n = input( 'Enter the number of successes: ')

Nmax = input( 'Enter the maximum number of trials: ')

x = []

y = []

for N in range( n, Nmax + 1 ):

x.append( N )

y.append( probability( prob, n, N )

plt.figure()

plt.title( 'Varying N and probability p = ' + str( prob ) )

plt.xlabel( 'N' )

plt.ylabel( 'Probability' )

plt.grid( True )

plt.plot( x, y )

plt.show()

plt.close()

def probability( prob, n, N ):

nCr = math.factorial( N ) / ( math.factorial( n ) * math.factorial( N - n ) )

P = nCr * ( prob**n ) * ( 1 - prob )**( N - n )

return P

2 Comments

y.append( probability( prob, n, N )

should be

y.append( probability( prob, n, N ))

This is a rather classical issue with syntax checking.

The parser doesn't know whether you forgot the closing parenthesis or whether you wanted to write a multi-line statement.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Join the community of 500,000 technology professionals and ask your questions.