Python bridge - wrong output sample

Posted By: Grat

Python bridge - wrong output sample - 06/30/19 09:35

Hi,

I trying the python sample:
Code

   if(!pyStart(0,1)) {
    printf("Error - Python won't start!");
    return;
  }


  var Vec[5] = { 0,1,2,3,4 };
  pySet("PyVec",Vec,5);
  pyX("for i in range(5): PyVec[i] *= 10\n");
  pyVec("PyVec",Vec,5);

  int i;
  printf("\nReturned: ");
  for(i=0; i<5; i++) printf("%.0f ",Vec[i]);

// test a function
  
  pyX("def PySum(V):\n Sum = 0.0\n for X in V:\n Sum += X\n return Sum\n\n");
  pyX("Result = PySum(PyVec)");
  printf("\nSum: %.0f",pyVar("Result"));


output the Sum is -1, but correct is 100
Posted By: Spirit

Re: Python bridge - wrong output sample - 06/30/19 10:29

Maybe not var but int?
Posted By: Grat

Re: Python bridge - wrong output sample - 06/30/19 15:29

no, becouse is:
Code
...
Sum = 0.0
....


this working (return 100):
Code
pyX("Sum=0");
pyX("for X in PyVec: Sum += X");


this code also work:
Code
  pyX("def PySum(V):\n\tSum=0\n\tfor X in V:\n\t\tSum =Sum+ X\n\treturn( Sum)\n\n");
  pyX("Sum=PySum(PyVec)");
  printf("\nSum: %f",pyVar("Sum"));


so, is a better use \t not space,
© 2024 lite-C Forums