Another example.
- Get first number from the user (as string).
- Convert to double.
- Get second number from the user (as string).
- Convert to double.
- Create Python scope.
- In scope, define a Python function 'add'.
- Create C# var 'sum', equal to the return value of scope.add(firstNumber, secondNumber).
- Print 'sum'.
/*/ nuget PyNet\PythonNet; /*/
using Python.Runtime;
PythonEngine.Initialize();
using (Py.GIL()) {
// NOTE: this doesn't validate input
if (!dialog.showInput(out string firstCsharpNumber, "Enter first number:")) return;
PyObject firstNumber = Convert.ToDouble(firstCsharpNumber).ToPython();
if (!dialog.showInput(out string secondCsharpNumber, "Enter second number:")) return;
PyObject secondNumber = Convert.ToDouble(secondCsharpNumber).ToPython();
dynamic scope = Py.CreateScope();
scope.Exec("def add(a, b): return a + b");
var sum = scope.add(firstNumber, secondNumber);
print.it("Sum: " + sum);
}
PythonEngine.Shutdown();