Error is not an Error?
06-17-2021, 05:13 AM (This post was last modified: 06-17-2021 05:31 AM by toml_12953.)
Post: #1
 toml_12953 Senior Member Posts: 2,145 Joined: Dec 2013
Error is not an Error?
When I enter the code blow, I check it and get no errors in the HPPL editor. When I try to run it, however, I get a syntax error in line 51. It works in CPython.

Traceback (most recent call last):
File "<stdin>", line 51
SyntaxError: invalid syntax

Code:
#PYTHON name # cubic from math import * # compute real or complex roots of cubic polynomial def cubic( a2, a1, a0 ):     global z1,z2,z3     Q = (3*a1 - a2**2)/9     R = (9*a1*a2 - 27*a0 - 2*a2**3)/54     D = Q**3 + R**2                        # polynomial discriminant     if (D >= 0):                           # complex or duplicate roots         S = sgn(R + sqrt(D))*abs(R + sqrt(D))**(1/3)         T = sgn(R - sqrt(D))*abs(R - sqrt(D))**(1/3)         z1 = -a2/3 + (S + T)               # real root         z2 = -a2/3 - (S + T)/2             # real part of complex root         z3 = -a2/3 - (S + T)/2             # real part of complex root         im = abs(sqrt(3)*(S - T)/2)        # complex part of root pair     else:                                  # distinct real roots         th = acos(R/sqrt( -Q**3))                  z1 = 2*sqrt(-Q)*cos(th/3) - a2/3         z2 = 2*sqrt(-Q)*cos((th + 2*pi)/3) - a2/3         z3 = 2*sqrt(-Q)*cos((th + 4*pi)/3) - a2/3         im = 0     return im                               # imaginary part # sign of number def sgn( x ):     if x < 0.0:         return -1     return 1 print("solve: x^3 + a*x^2 + b*x + c = 0") a,b,c = input("a,b,c? ").split(",") a=int(a) b=int(b) c=int(c) im = cubic( a, b, c ) if im != 0.0:     print("{",f"{z1:6.4f}, {z2:6.4f} + {im:6.4f}j, {z3:6.4f} - {im:6.4f}j","}") else:     print("{",f"{z1:6.4f}, {z2:6.4f}, {z3:6.4f}","}")      #end EXPORT cubic() BEGIN   PYTHON(name); END;

Tom L
Cui bono?
06-17-2021, 08:48 AM
Post: #2
 Liamtoh Resu Member Posts: 122 Joined: May 2021
RE: Error is not an Error?
I was able to reproduce your results.

BTW your code runs ok with python 3.9.4 on the pc after PPL wrapper code is removed.

My guess is that the hp prime python may not support the split and formatting codes,
but I will have to create some small programs to confirm or refute that theory.

What is the HPPL editor?

Thanks.
06-17-2021, 10:46 AM
Post: #3
 Albert Chan Senior Member Posts: 2,686 Joined: Jul 2018
RE: Error is not an Error?
HP Prime Python (micro Python?) might not support f string

Try string interpolation with format(), or %

On Python 2.6:

>>> z1, z2, z3, im = 1,2,3,4
>>> print "{ %6.4f, %6.4f + %6.4fj, %6.4f - %6.4fj }" % (z1,z2,im,z3,im)
{ 1.0000, 2.0000 + 4.0000j, 3.0000 - 4.0000j }
06-17-2021, 11:32 AM (This post was last modified: 06-17-2021 11:53 AM by Thomas_Sch.)
Post: #4
 Thomas_Sch Senior Member Posts: 409 Joined: Dec 2013
RE: Error is not an Error?
HP-Prime python is micropython. (http://micropython.org/; https://docs.micropython.org)
Micropython is based on Python 3.4. (see Help > Tree > HP apps > Python app > OK, page 2)
You may check this:
Code:
from sys import * print(version_info)

F-string literals have been added to python since python 3.6.
You can check more about it here (https://www.python.org/dev/peps/pep-0498/)
(from https://stackoverflow.com/questions/5040...yntaxerror)
06-17-2021, 02:28 PM
Post: #5
 toml_12953 Senior Member Posts: 2,145 Joined: Dec 2013
RE: Error is not an Error?
(06-17-2021 08:48 AM)Liamtoh Resu Wrote:  I was able to reproduce your results.

BTW your code runs ok with python 3.9.4 on the pc after PPL wrapper code is removed.

My guess is that the hp prime python may not support the split and formatting codes,
but I will have to create some small programs to confirm or refute that theory.

What is the HPPL editor?

Thanks.

From Home screen, press Shift then 1. Now press New and you're in the HPPL editor.

Tom L
Cui bono?
06-17-2021, 02:57 PM
Post: #6
 toml_12953 Senior Member Posts: 2,145 Joined: Dec 2013
RE: Error is not an Error?
(06-17-2021 11:32 AM)Thomas_Sch Wrote:  HP-Prime python is micropython. (http://micropython.org/; https://docs.micropython.org)
Micropython is based on Python 3.4. (see Help > Tree > HP apps > Python app > OK, page 2)
You may check this:
Code:
from sys import * print(version_info)

F-string literals have been added to python since python 3.6.
You can check more about it here (https://www.python.org/dev/peps/pep-0498/)
(from https://stackoverflow.com/questions/5040...yntaxerror)

OK, I changed the format statements to the old method. Now I get an error on the input() statement. I try to enter -2,1,-2 and it enters Ans-2 for the first number.

Tom L
Cui bono?
06-17-2021, 03:01 PM (This post was last modified: 06-17-2021 03:07 PM by Didier Lachieze.)
Post: #7
 Didier Lachieze Senior Member Posts: 1,641 Joined: Dec 2013
RE: Error is not an Error?
(06-17-2021 02:57 PM)toml_12953 Wrote:  OK, I changed the format statements to the old method. Now I get an error on the input() statement. I try to enter -2,1,-2 and it enters Ans-2 for the first number.

You need to enter -2 with [2] [+/-] or [+/-] [2], not with [-] [2]. If you start the entry with an operator key (+, -, x, /) the Prime will assume you want to apply this operator to the previous result (Ans).
06-17-2021, 06:30 PM (This post was last modified: 06-18-2021 03:07 AM by Liamtoh Resu.)
Post: #8
 Liamtoh Resu Member Posts: 122 Joined: May 2021
RE: Error is not an Error?
Here is a small python program that calculates the average of three numbers
entered by the user.

Code:
 #PYTHON name # avg3nr02 import math def pintro():   print("average three numbers") def enter_3():    a,b,c = input("a,b,c? ").split(",")    print(" " + a + ", " + b + ", " + c)    return (int(a)+ int(b) +int(c))/3    def pout():    q = enter_3()    print("the average is ",q)     pintro() pout() #end    EXPORT avg3nr02() BEGIN   PRINT;   PYTHON(name); END;

And another version that provides formatted output.

Code:
 #PYTHON name # avg3nr03 import math def pintro():   print("average three numbers") def enter_3():    a,b,c = input("a,b,c? ").split(",")    print(" " + a + ", " + b + ", " + c)    return (float(a)+ float(b) + float(c))/3    def pout():    q = enter_3()    print("the average is {0:.5f}".format(q))     pintro() pout() #end    EXPORT avg3nr03() BEGIN   PRINT;   PYTHON(name); END;

And thanks for the nomenclature for HPPL.

Thanks.
06-20-2021, 03:25 PM
Post: #9
 Albert Chan Senior Member Posts: 2,686 Joined: Jul 2018
RE: Error is not an Error?
(06-17-2021 03:01 PM)Didier Lachieze Wrote:  You need to enter -2 with [2] [+/-] or [+/-] [2], not with [-] [2]. If you start the entry with an operator key (+, -, x, /) the Prime will assume you want to apply this operator to the previous result (Ans).

You can also enter a space before [-] [2]
 « Next Oldest | Next Newest »

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