newbie Python

860
5
12-16-2010 11:57 AM
DeborahKapell
New Contributor
I'm converting a model from v9 to v10 so I have to convert some code from VBA to Python.  I'm trying to take a small number from variable Joint_Count, create a rate per 100,000 and put the results in a field called CountProp.  I can't divide into 0 so when Joint_Count=0, CountProp should also be 0.

VBA code that worked:
dim CountP as double
if [Join_count] = 0 then
CountP=0
else countP= ([Join_count] / [POP2005])*100000
end if

Python:
Field name: CountProp
Expression: doCount(!Join_Count!)

code (the last of many iterations)

def doCount(Join_Count):
  if  !Join_Count! = 0:
     tempnum=0:
  elif !Join_Count!>0:
     TempNum=(!Join_Count!/!POP2005!)*100000
  return TempNum

I get error 000989, parsing error: invalid syntax (line 3)

Any help would be appreciated
Tags (2)
0 Kudos
5 Replies
JasonScheirer
Occasional Contributor III
tempnum == 0, you can't use = in an if statement. Double equals for equality test.
0 Kudos
DeborahKapell
New Contributor
there must be more than one error. 

def doCount(Join_Count):
  if  !Join_Count! == 0:
    TempNum=0
  else:
    TempNum=(!Join_Count!/!POP2005!)*100000
  return TempNum

I get ERROR 000989
Python syntax error: Parsing error : invalid syntax (line 2)
0 Kudos
LoganPugh
Occasional Contributor III
Remove the exclamation points from your Join_Count and POP2005 variables within the Python code (they are only valid in the expression box).

Also, in your function definition, you'll want to add an argument for POP2005.

So something like this (the variable names Join_Count and POP2005 can be anything you want):

def doCount(Join_Count, POP2005):


You'll also need to update your expression to call the doCount function with both arguments.

doCount(!Join_Count!, !POP2005!)
0 Kudos
MarkShymanski
New Contributor III
there must be more than one error. 

def doCount(Join_Count):
  if  !Join_Count! == 0:
    TempNum=0
  else:
    TempNum=(!Join_Count!/!POP2005!)*100000
  return TempNum

I get ERROR 000989
Python syntax error: Parsing error : invalid syntax (line 2)


As mentioned else where you need to be clear on your usage of equivalency (denoted by '==') versus assignment (denoted by '=').  Your if and elif statments are testing for equivalency not assigning a value so you would need something like:

if (Join_Count == 0):
     [INDENT]do something[/INDENT]
elif (Join_Count > 0):
      [INDENT]do something else[/INDENT]
0 Kudos
MarkShymanski
New Contributor III
there must be more than one error. 

def doCount(Join_Count):
  if  !Join_Count! == 0:
    TempNum=0
  else:
    TempNum=(!Join_Count!/!POP2005!)*100000
  return TempNum

I get ERROR 000989
Python syntax error: Parsing error : invalid syntax (line 2)


As mentioned else where you need to be clear on your usage of equivalency (denoted by '==') versus assignment (denoted by '=').  Your if and elif statments are testing for equivalency not assigning a value so you would need something like:

if (Join_Count == 0):
     [INDENT]do something[/INDENT]
elif (Join_Count > 0):
      [INDENT]do something else[/INDENT]
0 Kudos