Problem with python program
11-03-2023, 09:30 PM
Post: #1
 Simon_arg Junior Member Posts: 3 Joined: Nov 2023
Problem with python program
Hello everyone,
I recently bought a new G2 and ahve been blown away by how much it can do. However I have been having some problems when trying to load and run some simple python programs I have created. My program is basically a simple algorithm to compute integrals using simpsons rule or trapezium rule. It is written in python 3.11.4 but only uses basic operations, as I input the amount of nodes and the value for the function in said nodes, and it outputs the calculation results. The code works correctly in my computer but it refuses to run in the calculator because of a 'Syntax error'. I am also kind of new to python as a whole, and I have read that programs might need to be modified to run on the g2. If anyone knows how to make this work or if my program wont't run on the g2 I would love to know. I have copied the code below just in case. Thanks in advance.[/code]

def simpson(h,suma,cant):
if cant%2!=1:
print('Debe ingresar un numero impar de nodos')
else:
for i in range(1,cant+1):
print('Ingrese la imagen del nodo numero ',i)
x=float(input())
if i==1 or i==cant:
suma += x
elif i%2==1:
suma += 2*x
elif i%2==0:
suma += 4*x
final = (h/3)*(suma)
print('El resultado numerico es: ',final)
return final

def trapezio(h,suma,cant):
for i in range(1,cant+1):
print('Ingrese la imagen del nodo numero ',i)
x=float(input())
if i==1 or i==(cant):
suma +=x
else:
suma+=2*x
final=(h/2)*(suma)
print('El resultado numerico es: ',final)
return final

suma = 0
m=int(input('Ingresar 1 para simpson y 2 para trapezio'))
h=float(input('Distancia entre nodos (h): '))
cant=int(input('Ingrese la cantidaad de nodos a usar (n+1): '))

if m==1:
elif m==2:
11-04-2023, 01:11 PM
Post: #2
 BruceH Senior Member Posts: 503 Joined: Dec 2013
RE: Problem with python program
And this is exactly why the decision to make Python reliant on indentation was so <bleah>.

Not your fault, of course, Simon_arg, but could you edit your post (or start again) and use the [​code] .. [​/code] tags (which you get by clicking on the # symbol in the editor toolbar) so that your indentation is preserved?
11-04-2023, 03:29 PM
Post: #3
 Thomas Klemm Senior Member Posts: 2,005 Joined: Dec 2013
RE: Problem with python program
Auto-formatted using black:
Code:
def simpson(h, suma, cant):     if cant % 2 != 1:         print("Debe ingresar un numero impar de nodos")     else:         for i in range(1, cant + 1):             print("Ingrese la imagen del nodo numero ", i)             x = float(input())             if i == 1 or i == cant:                 suma += x             elif i % 2 == 1:                 suma += 2 * x             elif i % 2 == 0:                 suma += 4 * x         final = (h / 3) * (suma)         print("El resultado numerico es: ", final)     return final def trapezio(h, suma, cant):     for i in range(1, cant + 1):         print("Ingrese la imagen del nodo numero ", i)         x = float(input())         if i == 1 or i == (cant):             suma += x         else:             suma += 2 * x     final = (h / 2) * (suma)     print("El resultado numerico es: ", final)     return final suma = 0 m = int(input("Ingresar 1 para simpson y 2 para trapezio")) h = float(input("Distancia entre nodos (h): ")) cant = int(input("Ingrese la cantidaad de nodos a usar (n+1): ")) if m == 1:     resultado = simpson(h, suma, cant) elif m == 2:     resultado = trapezio(h, suma, cant)

In the simpson function the first indent is 8 instead of 4 blanks.

(11-04-2023 01:11 PM)BruceH Wrote:  And this is exactly why the decision to make Python reliant on indentation was so <bleah>.

Just use an editor that replaces tabs with 4 blanks.

The problem here was that quote and code blocks were mixed.
11-04-2023, 03:34 PM
Post: #4
 komame Senior Member Posts: 307 Joined: Aug 2015
RE: Problem with python program
The following is a slightly improved version to make the entered values visible on the HP Prime and to have line breaks for enhanced readability.

Code:
def simpson(h, suma, cant):     if cant % 2 != 1:         print('Debe ingresar un numero impar de nodos')     else:         for i in range(1, cant + 1):             x = float(input('Ingrese la imagen del nodo numero %i: ' %i))              print(x)             if i == 1 or i == cant:                 suma += x             elif i % 2 == 1:                 suma += 2 * x             elif i % 2 == 0:                 suma += 4 * x         final = (h / 3) * (suma)         print('El resultado numerico es:', final)         return final def trapezio(h, suma, cant):     for i in range(1, cant + 1):         x = float(input('Ingrese la imagen del nodo numero %i: ' %i))                         print(x)          if i == 1 or i == cant:             suma += x         else:             suma += 2 * x     final = (h / 2) * (suma)     print('El resultado numerico es:', final)     return final suma = 0 m = int(input('Ingresar 1 para simpson y 2 para trapezio: ')) print(m) h = float(input('Distancia entre nodos (h): ')) print(h) cant = int(input('Ingrese la cantidad de nodos a usar (n+1): ')) print(cant) if m == 1:     resultado = simpson(h, suma, cant) elif m == 2:     resultado = trapezio(h, suma, cant)
11-07-2023, 12:19 AM
Post: #5
 Simon_arg Junior Member Posts: 3 Joined: Nov 2023
RE: Problem with python program
Thanks for all the help komame, Thomas Klemm and BruceH. Sorry for not getting back to you earlier. I have tried running the program using the corrected code which both of you have proposed but I still cannot get the program to run. The calculator shows an error message for syntax error and hitting the run button just shows a yellow exclamation mark as if it will not run. I am loading the code into the g2 using the connectivity kit using the programs section, and creating a new program. Is this the correct way to it? Maybe the errors are just down to how I'm loading the program. Thanks again.
11-08-2023, 07:32 AM
Post: #6
 komame Senior Member Posts: 307 Joined: Aug 2015
RE: Problem with python program
(11-07-2023 12:19 AM)Simon_arg Wrote:  The calculator shows an error message for syntax error and hitting the run button just shows a yellow exclamation mark as if it will not run. I am loading the code into the g2 using the connectivity kit using the programs section, and creating a new program. Is this the correct way to it? Maybe the errors are just down to how I'm loading the program. Thanks again.

HP Prime has two methods for running Python programs. The first one works through the PYTHON application (the Python icon when you press the APPS button on the calculator). In this case, in the Connectivity Kit, you need to expand the tree under "Application Library" => "Python" => "Files" and place your *.py file there.

The second method works by using a PPL wrapper. In this case, in the Connectivity Kit, you need to expand the "Programs". However, since this method uses a different language (PPL), which doesn't inherently understand Python, you have to use the PPL wrapper as described here: [How to run a specific python program or file ?]. Based on what you're describing, you're probably trying to use the second method, but you're attempting to run a Python program directly without the wrapper.

Piotr Kowalewski
11-08-2023, 11:35 PM
Post: #7
 Simon_arg Junior Member Posts: 3 Joined: Nov 2023
RE: Problem with python program
Thanks again Komame, the program is now working perfectly.
 « Next Oldest | Next Newest »

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