Python plotting

Posted on 2013-01-10
Last Modified: 2013-01-10

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


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.title( 'Varying N and probability p = ' + str( prob ) )
      plt.xlabel( 'N' )
      plt.ylabel( 'Probability' )
      plt.grid( True )
      plt.plot( x, y )

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
Question by:IssacJones
LVL 16

Accepted Solution

gelonida earned 300 total points
ID: 38762646
You forgot a closing parenthesis in the line before.

            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.

Author Closing Comment

ID: 38762795

Featured Post

Gigs: Get Your Project Delivered by an Expert

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.

Question has a verified solution.

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

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

776 members asked questions and received personalized solutions in the past 7 days.

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

Join & Ask a Question