所以我安裝了 IronPython 3.4 來替換我正在使用的 IronPython 2.7。下面的代碼在 2.7 中運行良好,但是當我在 3.4 中使用它時出現錯誤: Microsoft.Scripting.SyntaxErrorException: 'invalid syntax' 有什么想法嗎?謝謝。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
Microsoft.Scripting.Hosting.ScriptEngine pythonEngine = IronPython.Hosting.Python.CreateEngine();
Microsoft.Scripting.Hosting.ScriptSource pythonScript = pythonEngine.CreateScriptSourceFromString("print 'Hello World!'");
pythonScript.Execute();
}
}
}
嘗試了上面的代碼,它沒有作業,但在 IronPython 2.7 中作業。
uj5u.com熱心網友回復:
IronPython 3.4使用 python 3.4 語法,而 asIronPython 2.7使用 python 2.7。
在 python 2.7print中是一個陳述句,而在 python 3print中是一個函式(這意味著您需要使用括號來包裝引數)。因此,要使您的代碼正常作業,您必須將以下部分從
pythonEngine.CreateScriptSourceFromString("print 'Hello World!'");
^^^^^^^^^^^^^^^^^^^^
至
pythonEngine.CreateScriptSourceFromString("print('Hello World!')");
^^^^^^^^^^^^^^^^^^^^^
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/527376.html
