Post Reply 
new user python on hp prime g2, casio cg50
05-29-2021, 08:09 AM (This post was last modified: 05-29-2021 08:24 AM by drbarnack.)
Post: #14
RE: new user python on hp prime g2, casio cg50
(05-26-2021 06:28 AM)drbarnack Wrote:  Following on this, is there a way to return the output from a Python script embedded in a PPL program to the stack?

For example, this program will print the output to the Terminal:
Code:

#PYTHON test
#test.py
import sys
 
def sqrx(x):
  return int(x)**2
   
y = sqrx(sys.argv[0])
print(y)

# Return the value of y to the stack? 

#end 
 
EXPORT PYTEST(a)
BEGIN
  PYTHON(test,a);
END;

-DrBarnack

I figured this out. The trick is to evaluate the output from the Terminal:

Code:

#PYTHON test
#test.py
import sys
 
def sqrx(x):
  """
  Square a number.
  """
  return int(x)**2

# get the number on the stack and square it
y = sqrx(sys.argv[0])
# print the result to the Terminal
print(y)

#end 
 
EXPORT PYTEST(a)
BEGIN
  \\ clear the Terminal
  PRINT;
  \\ run the Python script
  PYTHON(test,a);
  \\evaluate the string in the Terminal and return to the stack
  RETURN EXPR(TERMINAL(1));
END;

This takes a value from the stack ("sys.argv[0]"), does the calculation in Python, prints the answer to the Terminal, parses the first string in the Terminal into an expression and evaluates it, leaving the answer on the stack for future use ("RETURN EXPR(TERMINAL(1));"). So in Home or CAS mode I can use Python scripts like any other function in the Toolbox to manipulate values.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: new user python on hp prime g2, casio cg50 - drbarnack - 05-29-2021 08:09 AM



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