HP Forums

Full Version: Implementing Python var=input("prompt") in HP42S
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'd like to support Python's
myvar = input()
myvar = input("some prompt")
syntax in my Python to HP42S RPN converter website

Currently Python's input() command is not supported at all by the converter. You have to use the native HP42S command INPUT, which is ok, since you can access all HP42S functions from the Python converter by invoking them as uppercase functions. Thus type INPUT(myvar) into the converter to get the RPN output INPUT "myvar". So it would be trivial for me to implement the conversion of the Python native expression myvar = input("prompt") as INPUT "myvar", which will run nicely on the HP42S, DM42, Free42.

So far so good. Where it gets interesting is that Python allows you to specify a parameter to the input function, which is the prompt. I'm thinking of implementing it in RPN like this:

"enter value>"
STO "myvar"

a more sophisticated implementation would display the old value of the variable underneath the prompt, cater for nonexistent variables (hence the flag 25 magic) and clean up the stack as best I can afterwards, thus:

SF 25
RCL "myvar"
FC?C 25
"enter value>"
STO "myvar"

If anybody has any thoughts on improving this implementation, please comment.

Thus the full future experience would convert the Python
# Python code for use in www.pyrpn.atug.com
myval = input("enter value>")
print("val was", myval)
into the runnable RPN (try pasting the below into Free42)
00 SF 25
00 RCL "myvar"
00 FC?C 25
00 0
00 "enter value>"
00 STO "myvar"
00 RDN
00 RDN
00 "val was"
00 ├" "
00 ARCL "myvar"
00 RTN

This enhancement proposal should allow people who are typing the Python input() command to get a useful result, rather than being puzzled why that particular bit of syntax is not being converted properly. Its arguably a common enough idiom that it should be supported.
Reference URL's