1

I have a python script with some parameters - see screen. enter image description here

You see the last parameter is of type Double, default Value 0.

The code i have this:

Ausrichtung = float(arcpy.GetParameterAsText(9))
if Ausrichtung == '#' or not Ausrichtung:
    Ausrichtung = "0.0"
# maybe there is some useless ballast...it comes from an export from modelbuilder, and I am learning python now.

arcpy.AddMessage("Ausrichtung"+ Ausrichtung)
SteigungX = -math.sin(math.radians(Ausrichtung))

1) When I start the script - in the form i let for this parameter a 0, i always get: Ausrichtung0.0 TypeError: a float is required 2) If I start the schript - with this parameter as a 5, it works. 3) When I replace last line with SteigungX = -math.sin(math.radians(float(Ausrichtung))) it works with all values, also with 0

I have several more parameters before this, and I can calculate with them.

Why is Zero so different?

Why do I have Ausrichtung in case 1) a 0, even when I use this float function?

AddMessage give a 0.0, so why still not recognized as float?

closed as too broad by PolyGeo Jul 24 '17 at 0:42

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

4

Try replacing this:

Ausrichtung = float(arcpy.GetParameterAsText(9))
if Ausrichtung == '#' or not Ausrichtung:
    Ausrichtung = "0.0"
arcpy.AddMessage("Ausrichtung"+ Ausrichtung)

with this:

Ausrichtung = arcpy.GetParameterAsText(9)  # do not float() until you know it's numeric
if Ausrichtung == '#' or not Ausrichtung:
    Ausrichtung = 0.0  # a float, not a string
else:
    Ausrichtung = float(Ausrichtung)
arcpy.AddMessage("Ausrichtung {0}".format(Ausrichtung))
  • Thank you, meanwhile I got more Ideas: - the "numeric" is given through the input form (i can´t enter nothing but a number in the input field) - so I can use float(arcpy.Getparameter(9)) - I can use "GetParameter" (GetParameterAsText seems not to be necessary) - 0 means false, so it goes to the If Condition - you are right - not "0.0" but 0.0 makes a float This code works now and should be safe: Ausrichtung = float(arcpy.GetParameter(9)) if Ausrichtung == '#' or not Ausrichtung: Ausrichtung = 0.0 Correct in all points? – Michael Glöckner Jan 14 '15 at 16:38
  • 2
    In that case, the whole thing can be reduced to Ausrichtung = arcpy.GetParameter(9) if arcpy.GetParameter(9) else 0.0 (all on one line), and everything from the if onward is only necessary if it's possible to pass in null/blank values instead of 0. – nmpeterson Jan 14 '15 at 16:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.