Problem Description: Using C # to call the following method parameters written in python and want to get the return value.
def Quadratic_Equations(a,b,c): D=b**2-4*a*c years = [] ans.append((-b+math.sqrt(D))/(2*a)) ans.append (( -b-math.sqrt (D)) / (2 * a)) return years
C # code is as follows:
class Program { static void Main(string[] args) { string name = "CallPythonExam.py"; List<string> param = new List<string>() { "3", "5", "1" }; RunPythonScript(name, param); } static void RunPythonScript(string name, List<string> args) { The p-Process = new new Process (); // absolute path .py file String path = @ " D: \ PythonPrograms \ VelocityProfile \ VelocityProfile \ " + name; String arguments The = path; // add parameters foreach ( var Item in args) arguments The + = " " + Item; // Python installation path p.StartInfo.FileName = @ " C: \ Program Files (x86) \ in the Microsoft Visual Studio \ Shared \ Python36_64 \ python.exe " ; // The following is what I copied directly from the Internet down ....... p.StartInfo.Arguments = arguments The; // does not enable the shell to start the process p.StartInfo.UseShellExecute = false ; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardError = to true ; // without creating a new window p.StartInfo.CreateNoWindow = to true ; p.Start(); StreamReader sr = p.StandardOutput; while (!sr.EndOfStream) Console.WriteLine(sr.ReadLine()); Console.ReadLine(); p.WaitForExit(); } }
python code is as follows:
import math import sys def Quadratic_Equations(stra,strb,strc): a=float(stra) b=float(strb) c=float(strc) D=b**2-4*a*c years = [] ans.append((-b+math.sqrt(D))/(2*a)) ans.append((-b-math.sqrt(D))/(2*a)) print(ans[0]) print(ans[1]) #return str(ans[0]) Quadratic_Equations(sys.argv[1],sys.argv[2],sys.argv[3])
This solution of the equation can be output on the screen.