HP Forums
Implementing Python var=input("prompt") in HP42S - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: Not HP Calculators (/forum-7.html)
+--- Forum: Not quite HP Calculators - but related (/forum-8.html)
+--- Thread: Implementing Python var=input("prompt") in HP42S (/thread-11655.html)



Implementing Python var=input("prompt") in HP42S - tcab - 10-25-2018 12:12 AM

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.