How does python work? - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: HP Prime (/forum-5.html) +--- Thread: How does python work? (/thread-17438.html) |
How does python work? - hamorabi - 09-06-2021 04:37 PM I recently upgraded my Prime to the latest firmware and I'm trying to understand how to write python functions/scripts. I didn't seem to find much a solution while perusing older threads. Let's say my script looks like this: PHP Code: def func1(a, b): I know this is a contrived example, but I just want to know how to make it work. The question is, how do I call func3 ? RE: How does python work? - Dougggg - 09-06-2021 04:53 PM This will work Code: def func1(a, b): return a + b def func2(a, b): return a - b def func3(a, b, c): if c == 0: return func1(a, b) else: return func2(a, b) d=func3(4,2,0) RE: How does python work? - hamorabi - 09-06-2021 05:01 PM (09-06-2021 04:53 PM)Dougggg Wrote: This will work Maybe, I didn't explain properly. I am not looking to run the code from the python app. I am looking to run the code from the place where you do regular calculations. For example, if I was using python-syntax under XCas, I would do the following: PHP Code: #cas I'm looking to do the same but with python, not XCas. RE: How does python work? - roadrunner - 09-06-2021 08:15 PM Just paste your program exactly as written into a prime program file: [attachment=9790] tap <check> and exit out. Typing func3(1,2,3) on the command line returns -1. -road RE: How does python work? - hamorabi - 09-07-2021 07:45 AM (09-06-2021 08:15 PM)roadrunner Wrote: Just paste your program exactly as written into a prime program file: Yes, but this doesn't run the program as a python code. It runs it as an XCAS program. It just so happens that XCAS can accept some python syntax. What I am asking how to run python code, not XCAS with python-like syntax. RE: How does python work? - roadrunner - 09-07-2021 12:06 PM Maybe the PYTHON command is what you are looking for? Code: #PYTHON test RE: How does python work? - hamorabi - 09-07-2021 09:24 PM (09-07-2021 12:06 PM)roadrunner Wrote: Maybe the PYTHON command is what you are looking for? Thanks for this. But how do you display the result in the calculations window like regular functions so that they can be used. At the moment, the result only shows in the terminal. RE: How does python work? - roadrunner - 09-07-2021 10:05 PM I think you would have to save it as a variable, then do what ever you want with the variable like in the following example: PHP Code: #PYTHON test You can also save it as a home variable with hpprime.eval instead of cas.caseval. -road RE: How does python work? - hamorabi - 09-08-2021 10:48 AM (09-07-2021 10:05 PM)roadrunner Wrote: I think you would have to save it as a variable, then do what ever you want with the variable like in the following example: Thanks for this. This seems more complicated than I hoped for. Is there some resource I can use to learn things like this? RE: How does python work? - roadrunner - 09-08-2021 11:08 AM I don't know of any resources. There may be a better way to do what you want but that's the best I could come up with. Everything I know about Python on the prime was learned by trial and error (mostly error), and by reading posts in this forum. -road |