Post Reply 
Implementing Python var=input("prompt") in HP42S
10-25-2018, 12:12 AM (This post was last modified: 11-04-2018 01:17 AM by tcab.)
Post: #1
Implementing Python var=input("prompt") in HP42S
I'd like to support Python's
Code:
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>"
PROMPT
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
0
"enter value>"
PROMPT
STO "myvar"
RDN
RDN

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

Thus the full future experience would convert the Python
Code:
# Python code for use in www.pyrpn.atug.com
LBL "PROMPTIN"
myval = input("enter value>")
print("val was", myval)
into the runnable RPN (try pasting the below into Free42)
Code:
00 LBL "PROMPTIN"
00 SF 25
00 RCL "myvar"
00 FC?C 25
00 0
00 "enter value>"
00 PROMPT
00 STO "myvar"
00 RDN
00 RDN
00 "val was"
00 ├" "
00 ARCL "myvar"
00 AVIEW
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.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)