2

Here is a code sample from a Console App I found here.

static void Main(string[] args)
    {

        int a = 1;
        int b = 2;

        Microsoft.Scripting.Hosting.ScriptEngine py = Python.CreateEngine(); // allow us to run ironpython programs
        Microsoft.Scripting.Hosting.ScriptScope s = py.CreateScope(); // you need this to get the variables
        py.Execute("x = "+a+"+"+b,s); // this is your python program
        Console.WriteLine(s.GetVariable("x")); // get the variable from the python program
        Console.ReadLine(); // wait for the user to press a button
    }

I am using IronPython to try and execute a Python Script stored in a an external file called GuessingGameOne.pyinside of a Console Application Window.

    import random   

def main():
    randomnumber = random.randint(1, 100)
    matched = False
    count = 0
    while (matched == False):
        inputnumber = input('Choose a number: ')        
        try:
            count = count+1
            inputnumber = int(inputnumber)
            if inputnumber > 0 and inputnumber < 101:
                comparenumber = inputnumber - randomnumber
                if comparenumber == 0:                
                    print("You guessed it. It took you " + str(count) + " guesses" )
                    matched = True
                elif comparenumber < 0:
                    print("Guess is too low")
                else:                
                    print("Guess is too high")
            else:
                print("Your guess must be between 1 and 100")

        except ValueError:
            print("Invalid Entry: Your guess must be a number")

if __name__ == "__main__":
   main()

How can I alter Main(string[] args) to call the script GuessingGameOne.py (shown above) and run it in a Console Window?

1
  • You should really try and understand what this piece of code is doing before posting a question to SO. Commented Feb 27, 2017 at 19:05

1 Answer 1

2

I haven't tested this code, but I found a similar answer here:

https://stackoverflow.com/a/30385400/5258056

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using IronPython.Runtime;

static void Main(string[] args)
        {
            ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null);
            ScriptRuntime runtime = new ScriptRuntime(setup);
            ScriptEngine engine = Python.GetEngine(runtime);
            ScriptSource source = engine.CreateScriptSourceFromFile("GuessingGameOne.py");
            ScriptScope scope = engine.CreateScope();

            // You don't need this section, but added in case
            // you decide to use it later.
            List<String> argv = new List<String>();
            //Do some stuff and fill argv
            argv.Add("foo");
            argv.Add("bar");
            engine.GetSysModule().SetVariable("argv", argv);

            // You will need this though....
            source.Execute(scope);
        }

What you are not doing is specifying a file location to execute.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.