Python exec() command

following problem:
i have a string i want to execute like:

string = “2*A+B”
usually it should work like result = exec string.
when i try that in dynamo i receive only error messages.

any clue?

Please show a screenshot…

Generally you cannot perform math on strings… (Neither in Python nor in Dynamo)

Posting the error and the code would definitely help. Exec should work fine inside of Dynamo:

image

You’re possibly trying to reference an argument before assigning it. Maybe having keyword arguments would be easier in that case?

2 Likes

But using strings will give wrong results, at least that’s was what I meant in my original reply :wink:
image

Alternatively you can import the “math” module in Python
image

dimitar thank you for your reply.
just found out that it is a problem with dynamo 2+
going back to 1.3 everything is working.
btw code is just the same as your smple :wink:
tx peter

dimitar, asfaik math is not capable of calculating strings… true?

image

weird thing it that dynamo doe not calculate but writes output multiple times

It seems that it is repeated the number of times that your formula states?
E.g. “NAF*2” is repeated twice…

Can you show your entire script and possibly the input from Dynamo if you have that? It would make it easier to understand… :slight_smile:

Exactly that is the Point ?!

Architekturbüro DI Peter Kompolschek
Pestalozzistr. 11/60
AT 9500 Villach
+43 664 46 17 873
www.kompolschek.at

If any of your input arguments are strings and you want to use math on them, you’ll first have to convert them to a numeric object. For example:

A = float(“1.23”)
B = float(“4.56”)

1 Like

dimitar thank you.
thought that even variables shall be string.
by converting them to numbers solved the problem