2
ここにはコンソールアプリケーションのコードサンプルがあります。hereが見つかりました。私がしようとすると、外部ファイルに保存されているのPythonスクリプトを実行するためにIronPythonのを使用していますPythonはコンソールアプリケーションで外部Pythonスクリプトを実行します
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
}
は、コンソールアプリケーションウィンドウの内側にGuessingGameOne.py
と呼ばれます。
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()
どのように私は(上図)スクリプトGuessingGameOne.py
を呼び出し、コンソールウィンドウでそれを実行するためにMain(string[] args)
を変更することができますか?
SOに質問を投稿する前に、このコードが何をしているのか本当に理解してください。 –