New to blocky, some python issues

64 views
Skip to first unread message

Daniel

unread,
Oct 15, 2022, 3:57:02 AM10/15/22
to Blockly
So, I have the blocky code

(print) (prompt for text with message) ("hello")

This runs fine, and it outputs this as python code

def text_prompt(msg):
  try:
    return raw_input(msg)
  except NameError:
    return input(msg)


print(text_prompt('abracadabra'))

However, this doesn't run in python (raw_input is undefined)

Do I need to import a certain library?

Neil Fraser

unread,
Oct 15, 2022, 5:01:43 AM10/15/22
to blo...@googlegroups.com
What's the exact error message that your copy of Python returns?  If 'raw_input' is undefined it is supposed to throw a 'NameError', which as you can see should be caught.  Also helpful in debugging would be the version of Python you are using.  I've just tested the code in 3.9.12 and it seems to work fine for me.  Maybe you are using something other than CPython?

Background: The generated code is designed to run in both Python 2 and Python 3.  In Python 2 the 'input' function evaluates the text and throws a SyntaxError if it's not mathematically valid, thus one needs to use 'raw_input' if one wants to accept random text.  In Python 3 the 'input' function does not do evaluation, and the 'raw_input' function does not exist.  Thus Blockly's generated code tries to use 'raw_input' and if it doesn't exist (throws NameError), then it switches to 'input'.

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/e0626605-7c04-41ab-a823-eb0985eecf5bn%40googlegroups.com.


--
Reply all
Reply to author
Forward
0 new messages