UpdateCursor issue: "can't multiply sequence by non-int of type 'float'

410
2
Jump to solution
11-15-2013 04:33 PM
DamonOsbourne
New Contributor II
Attempting to use an update cursor to update a field 'Cost' by multiplying the value in the field 'Area' by a value input by the user.  The trouble seems to be getting the variables "Price1" and "Price2" to work inside the cursor's If statements.  If I simply replace the variable with a number, such as 2, then the script works fine, but it really needs to multiply a variable that the user has entered.  The variable that the user enters will be a money value, so it's of type Double with 2 decimal places.  The error I get is "can't multiply sequence by non-int of type 'float'".  Here's a snippet of the code:
Price1 = arcpy.GetParameterAsText(0) Price2 = arcpy.GetParameterAsText(1)  fcName = "My_lyr" fields = ['Area','Description','Cost']  #Note that 'Area' is type DOUBLE, 'Description' is TEXT, and 'Cost' is DOUBLE  CostCursor = arcpy.da.UpdateCursor(fcName,fields)  for row in CostCursor:  if row[1] == "Category 1":   row[2] = (row[0] * Price1)  elif row[1] == "Category 2":   row[2] = (row[0] * Price2)  else:   row [2] = 0  CostCursor.updateRow(row)  del row del CostCursor
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor
Attempting to use an update cursor to update a field 'Cost' by multiplying the value in the field 'Area' by a value input by the user.  The trouble seems to be getting the variables "Price1" and "Price2" to work inside the cursor's If statements.  If I simply replace the variable with a number, such as 2, then the script works fine, but it really needs to multiply a variable that the user has entered.  The variable that the user enters will be a money value, so it's of type Double with 2 decimal places.  The error I get is "can't multiply sequence by non-int of type 'float'".  Here's a snippet of the code:
Price1 = float(arcpy.GetParameterAsText(0)) Price2 = float(arcpy.GetParameterAsText(1))  fcName = "My_lyr" fields = ['Area','Description','Cost']  #Note that 'Area' is type DOUBLE, 'Description' is TEXT, and 'Cost' is DOUBLE  CostCursor = arcpy.da.UpdateCursor(fcName,fields)  for row in CostCursor:  if row[1] == "Category 1":   row[2] = (row[0] * Price1)  elif row[1] == "Category 2":   row[2] = (row[0] * Price2)  else:   row [2] = 0  CostCursor.updateRow(row)  del row del CostCursor


The parameters created by the GetParameterAsText method are always type string and have to be explicitly converted to a float before using them in numeric calculations.  Just change the parameter variable assignment code to this:

Price1 = float(arcpy.GetParameterAsText(0))
Price2 = float(arcpy.GetParameterAsText(1))

This code assumes the script tool input types are set up to be required fields that only allow type double values to be entered to avoid having to do the parameter assignment and type validation checks in the code.

View solution in original post

0 Kudos
2 Replies
RichardFairhurst
MVP Honored Contributor
Attempting to use an update cursor to update a field 'Cost' by multiplying the value in the field 'Area' by a value input by the user.  The trouble seems to be getting the variables "Price1" and "Price2" to work inside the cursor's If statements.  If I simply replace the variable with a number, such as 2, then the script works fine, but it really needs to multiply a variable that the user has entered.  The variable that the user enters will be a money value, so it's of type Double with 2 decimal places.  The error I get is "can't multiply sequence by non-int of type 'float'".  Here's a snippet of the code:
Price1 = float(arcpy.GetParameterAsText(0)) Price2 = float(arcpy.GetParameterAsText(1))  fcName = "My_lyr" fields = ['Area','Description','Cost']  #Note that 'Area' is type DOUBLE, 'Description' is TEXT, and 'Cost' is DOUBLE  CostCursor = arcpy.da.UpdateCursor(fcName,fields)  for row in CostCursor:  if row[1] == "Category 1":   row[2] = (row[0] * Price1)  elif row[1] == "Category 2":   row[2] = (row[0] * Price2)  else:   row [2] = 0  CostCursor.updateRow(row)  del row del CostCursor


The parameters created by the GetParameterAsText method are always type string and have to be explicitly converted to a float before using them in numeric calculations.  Just change the parameter variable assignment code to this:

Price1 = float(arcpy.GetParameterAsText(0))
Price2 = float(arcpy.GetParameterAsText(1))

This code assumes the script tool input types are set up to be required fields that only allow type double values to be entered to avoid having to do the parameter assignment and type validation checks in the code.
0 Kudos
DamonOsbourne
New Contributor II
Yes, that was it.  Thank you.
0 Kudos