0

I've just started to learn IronPython and I tried the code below which returned IronPython.Runtime.UnboundNameException: 'name 'hello' is not defined'.
Code:

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);

Console run ok, then it asked me to Input string, and I typed into "hello". And then it fired off the above error message. Then I tried this one just to see if it does without input method:

py.Execute(@"x = 2 + 3", scope);
py.Execute("print 'result'," + scope.GetVariable("x"));

So that one was ok.

Can someone please explain why can I not retrieve a variable from "input" method? and why is it an UnboundNameException?

Many thanks!

14
  • If you're using Python 2, then you need to use raw_input, not input. Commented Apr 18, 2018 at 8:23
  • Why do you have c# tag in the question ? Commented Apr 18, 2018 at 8:24
  • 1
    is wrod a typo here: var input = scope.GetVariable("wrod"); Commented Apr 18, 2018 at 8:25
  • @srf yeah, it's a typo. Sorry about that! Commented Apr 18, 2018 at 8:30
  • @khelwood I tried raw_input and now it returns IronPython.Runtime.UnboundNameException: 'global name 'hello' is not defined'. But now this error is at the line where print method is. Commented Apr 18, 2018 at 8:31

1 Answer 1

2

Having never worked with ironpython the answer lies within your own code.

your code:

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

results in that last line saying py.Execute("print dog") ... but there is no dog varaible.

yet here:

py.Execute("print 'result'," + scope.GetVariable("x"));

You know to encapsulate text in quotes..

Id speculate that

py.Execute("print " + input);

Should read

py.Execute("print '" + input + "'");

Which results in print 'dog'

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

5 Comments

yes! You're absolutely right! It worked like a charm! Those bloody quotes! :))) Thanks for making my day! You're a star!
See you had the answer.. I havent ever coded in python, but you had the logic in front of you to answer it The reason print 'result',<somevariable contents> probably works is because its a comma not a +
Hah! My gosh! I didn't even notice that LOL that's because you're a pro and am a complete beginner. Thanks for pointing that out.
I now get to annoy people by adding pyton to my list .. awesome - age 16 I was sitting in a university area pootling about on unix, bloke next to me ranted, F'd and blinded, i gave in and asked what was wrong. He pointed at his code.. I suggested a fix, I was right.. i had heard of the language, never used it, still havent, and fixed his PhD project.. so.. now I have 2 .. go me
Good to have such folks like yourself on this website! Yous are an asset ;) Take care!

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.