c# - C#Python输入法返回错误

标签 c# python compiler-errors ironpython

我刚刚开始学习IronPython,并尝试了下面的代码,该代码返回了IronPython.Runtime.UnboundNameException: 'name 'hello' is not defined'
代码:

var py = Python.CreateEngine();
var scope = py.CreateScope();
py.Execute(@"word = input('Input string\n')", scope);
var input = scope.GetVariable("word");
py.Execute("print " + input);

控制台运行正常,然后要求我输入Input string,然后输入"hello"。然后,它触发了以上错误消息。然后,我尝试了这个,只是看看它是否没有input方法:
py.Execute(@"x = 2 + 3", scope);
py.Execute("print 'result'," + scope.GetVariable("x"));

这样就可以了。

有人可以解释一下为什么我不能从"input"方法中检索变量吗?为什么是UnboundNameException

非常感谢!

最佳答案

从未与ironpython一起工作过,答案就在您自己的代码中。

您的代码:

py.Execute(@"word = input('Input string\n')", scope); (I type in dog)
var input = scope.GetVariable("word");
py.Execute("print " + input);

导致最后一行显示py.Execute(“print dog”)...,但是没有狗变量。

还在这里:
py.Execute("print 'result'," + scope.GetVariable("x"));

您知道将文本封装在引号中。

我推测
py.Execute("print " + input);

应该读
py.Execute("print '" + input + "'");

这导致打印“狗”

关于c# - C#Python输入法返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49894685/

相关文章:

c++ - 错误 : this declaration has no storage or type specifier

c# - 抑制 Visual Studio 中所有项目的警告

python - 为什么我得到 '_SIGCHLDWaker' object has no attribute 'doWrite' in Scrapy?

c# - MVC 4 在区域内路由 "finding" Controller

python - 从日期时间中选择时间

python - 使用jinja2和python 3,得到 undefined variable 错误

C++ 错误 : No match for 'operator='

java - Java中斐波那契序列中的BufferedReader错误

c# - 'System.Runtime.InteropServices.SafeHandle'和 'System.Runtime.InteropServices.dll'中都存在 'System.Runtime.Handles.dll'类型

c# - 库将在哪个 .net 框架版本中运行