Post Reply 
HP Prime Python runs consecutive programs
09-22-2023, 07:42 PM
Post: #1
HP Prime Python runs consecutive programs
I have two scripts. When the first one is done, the second imports and runs. This does not occur on my Casio, Numworks or either TI calculators with Python. When the second one is done, it does not import back to the first file. Very strange.
Find all posts by this user
Quote this message in a reply
09-23-2023, 05:57 PM
Post: #2
RE: HP Prime Python runs consecutive programs
(09-22-2023 07:42 PM)Pellidon Wrote:  I have two scripts. When the first one is done, the second imports and runs. This does not occur on my Casio, Numworks or either TI calculators with Python. When the second one is done, it does not import back to the first file. Very strange.
Unfortunately the Prime doesn't allow for selected imports or runs. One work around is to enclose all programs in "def xx():" you do this on the Numworks too usually, don't you?

Consider you have 2 programs "main" and "another"

Then "main" may have as content:
Code:
def foobar():
    print("this is from main")

and "another" may have as content:
Code:
def foobar():
    print("this is from another")
Then upon starting Python you'll see both modules imported but not executed.

Entering "main.foobar()" will then show: "this is from main"
consequently
"another.foobar()" will then show: "this is from another"

That's a bit cumbersome, hopefully someone does a thorough repair on the Python implementation.

I'll elaborate a bit on this at the HHUC at Orlando.
HTH Günter
Find all posts by this user
Quote this message in a reply
09-24-2023, 03:50 PM (This post was last modified: 09-24-2023 03:51 PM by toml_12953.)
Post: #3
RE: HP Prime Python runs consecutive programs
(09-23-2023 05:57 PM)Guenter Schink Wrote:  
(09-22-2023 07:42 PM)Pellidon Wrote:  I have two scripts. When the first one is done, the second imports and runs. This does not occur on my Casio, Numworks or either TI calculators with Python. When the second one is done, it does not import back to the first file. Very strange.
Unfortunately the Prime doesn't allow for selected imports or runs. One work around is to enclose all programs in "def xx():" you do this on the Numworks too usually, don't you?

Consider you have 2 programs "main" and "another"

Then "main" may have as content:
Code:
def foobar():
    print("this is from main")

and "another" may have as content:
Code:
def foobar():
    print("this is from another")
Then upon starting Python you'll see both modules imported but not executed.

Entering "main.foobar()" will then show: "this is from main"
consequently
"another.foobar()" will then show: "this is from another"

That's a bit cumbersome, hopefully someone does a thorough repair on the Python implementation.

I'll elaborate a bit on this at the HHUC at Orlando.
HTH Günter

After an import, NumWorks will automatically run a program that is not included in a function.

If you import the program below, Numworks will automatically run the code and print 7. If you remove the # and indent the print statement, then nothing will run automatically when this code is imported.

Code:
def addit(x,y):
  return x+y

#def main():
print(addit(5,2))

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
09-24-2023, 07:14 PM
Post: #4
RE: HP Prime Python runs consecutive programs
(09-24-2023 03:50 PM)toml_12953 Wrote:  After an import, NumWorks will automatically run a program that is not included in a function.

If you import the program below, Numworks will automatically run the code and print 7. If you remove the # and indent the print statement, then nothing will run automatically when this code is imported.

Code:
def addit(x,y):
  return x+y

#def main():
print(addit(5,2))
Yes, but in Numworks you can select for each program whether it will be imported or not. A Prime Python app doesn't allow to select which module to import and which not, import all or nothing (<shift><Plot>

This behavior is what my comment is directed at.
Günter
Find all posts by this user
Quote this message in a reply
09-24-2023, 09:50 PM
Post: #5
RE: HP Prime Python runs consecutive programs
The Prime executes the import of the second file, not me or my code. I've never had to do your suggestion on the numwirks or any other calculator. My suspect is a bug in the implementation.
Find all posts by this user
Quote this message in a reply
09-25-2023, 03:00 PM
Post: #6
RE: HP Prime Python runs consecutive programs
These are the two scripts. Only difference between these and the Casio are the random imports.

Cone program. Calculates the sheet metal size for a rolled cone.
from math import *
import math

major = input("major dia=")
major = float(major)
#print(major)
minor = input("minor dia=")
minor = float(minor)
#print(minor)
height = input("height=")
height = float(height)
#print(height)
o = math.sqrt((height*major/(major-minor))**2+(major/2)**2)
i = math.sqrt((height*minor/(major-minor))**2+(minor/2)**2)
angle = 360*major/(2*o)
r_angle = angle*(pi/180)
if angle >180:
x = o+o*sin(((angle-180)*(pi/180))/2)
y = 2*o
else:
x = o-i*cos((angle/2*(pi/180)))
y = 2*o*sin((angle/2*(pi/180)))
print("radius 1 = {0:6.3f}".format(o))
print("radius 2 = {0:6.3f}".format(i))
print("angle = {0:6.2f}".format(angle))
print("b height = {0:6.3f}".format(x))
print("b width = {0:6.3f}".format(y))
if y>144:
print("blank oversized")

red.py is a list of the magic eight ball responses that comes up randomly when pressing enter.
from micropython import *
from urandom import *
from urandom import random
import random

eightlist = ["It is certain ",
"Reply hazy, try again",
"Don’t count on it",
"It is decidedly so",
"Ask again later",
"My reply is no",
"Without a doubt",
"Better not tell you now",
"My sources say no",
"Yes definitely",
"Cannot predict now",
"Outlook not so good",
"You may rely on it",
"Concentrate and ask again",
"Very doubtful",
"As I see it, yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes" ]

shake = ""

while True:
shake = input("Shake for answer ")
if shake == "":
print (random.choice(eightlist)
)
#print (eightlist[eightball])
else:
print ("Thanks for playing")
break

Cone runs then at the end, it displays <import> red.py and runs that program.
Find all posts by this user
Quote this message in a reply
09-28-2023, 09:31 PM (This post was last modified: 09-28-2023 09:35 PM by Guenter Schink.)
Post: #7
RE: HP Prime Python runs consecutive programs
One solution is: <SHIFT> <PLOT> that gives you access to the Python setup. There untick "Files will auto load when changed". Now you can run your programs simply by typing
"import cone" or "import red" or whatever names your modules have.
without quoting of course

Günter

PS: your code is garbled. Better use "insert formatted code"
Find all posts by this user
Quote this message in a reply
09-29-2023, 05:47 PM
Post: #8
RE: HP Prime Python runs consecutive programs
Thanks, that worked.

When I pasted the code in, my indents were deleted.

(09-28-2023 09:31 PM)Guenter Schink Wrote:  One solution is: <SHIFT> <PLOT> that gives you access to the Python setup. There untick "Files will auto load when changed". Now you can run your programs simply by typing
"import cone" or "import red" or whatever names your modules have.
without quoting of course

Günter

PS: your code is garbled. Better use "insert formatted code"
Find all posts by this user
Quote this message in a reply
09-29-2023, 06:31 PM
Post: #9
RE: HP Prime Python runs consecutive programs
(09-29-2023 05:47 PM)Pellidon Wrote:  When I pasted the code in, my indents were deleted.
That's why suggested to use formatted code. Look at the " # " above the forum editor window.
Günter
Find all posts by this user
Quote this message in a reply
09-29-2023, 08:36 PM (This post was last modified: 09-29-2023 08:38 PM by toml_12953.)
Post: #10
RE: HP Prime Python runs consecutive programs
(09-29-2023 05:47 PM)Pellidon Wrote:  Thanks, that worked.

When I pasted the code in, my indents were deleted.

(09-28-2023 09:31 PM)Guenter Schink Wrote:  One solution is: <SHIFT> <PLOT> that gives you access to the Python setup. There untick "Files will auto load when changed". Now you can run your programs simply by typing
"import cone" or "import red" or whatever names your modules have.
without quoting of course

Günter

PS: your code is garbled. Better use "insert formatted code"
Something like this?

Code:
from math import *
import math

major = input("major dia=")
major = float(major)
#print(major)
minor = input("minor dia=")
minor = float(minor)
#print(minor)
height = input("height=")
height = float(height)
#print(height)
o = math.sqrt((height*major/(major-minor))**2+(major/2)**2)
i = math.sqrt((height*minor/(major-minor))**2+(minor/2)**2)
angle = 360*major/(2*o)
r_angle = angle*(pi/180)
if angle >180:
  x = o+o*sin(((angle-180)*(pi/180))/2)
  y = 2*o
else:
  x = o-i*cos((angle/2*(pi/180)))
  y = 2*o*sin((angle/2*(pi/180)))
print("radius 1 = {0:6.3f}".format(o))
print("radius 2 = {0:6.3f}".format(i))
print("angle = {0:6.2f}".format(angle))
print("b height = {0:6.3f}".format(x))
print("b width = {0:6.3f}".format(y))
if y>144:
  print("blank oversized")

red.py is a list of the magic eight ball responses that comes up randomly when pressing enter.
Code:
from micropython import *
from urandom import *
from urandom import random
import random

eightlist = ["It is certain ",
"Reply hazy, try again",
"Don’t count on it",
"It is decidedly so",
"Ask again later",
"My reply is no",
"Without a doubt",
"Better not tell you now",
"My sources say no",
"Yes definitely",
"Cannot predict now",
"Outlook not so good",
"You may rely on it",
"Concentrate and ask again",
"Very doubtful",
"As I see it, yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes" ]

shake = ""

while True:
  shake = input("Shake for answer ")
    if shake == "":
      print (random.choice(eightlist))
#print (eightlist[eightball])
  else:
    print ("Thanks for playing")
    break

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
Post Reply 




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