HP Forums
Python, how to input a complex number - 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: Python, how to input a complex number (/thread-16837.html)



Python, how to input a complex number - Dirk.nl - 04-29-2021 02:36 PM

According to Python documents, you can enter a complex number like this; var=complex(input())
For example, 3+5j
The Python App gives an error message here.
Could someone for me test / try this and explain what I'm doing wrong ?


RE: Python, how to input a complex number - Stevetuc - 04-29-2021 04:02 PM

(04-29-2021 02:36 PM)Dirk.nl Wrote:  According to Python documents, you can enter a complex number like this; var=complex(input())
For example, 3+5j
The Python App gives an error message here.
Could someone for me test / try this and explain what I'm doing wrong ?
Looking online, it seems input cannot directly pass a complex number to complex().
I haven't tried this, but it's suggested to take a string as input then convert to complex number:
https://stackoverflow.com/a/57403645

Code:

a = input() # user will enter 3+5j 
a = complex(a) # then this will be converted into complex number.

Edit: hmm ignore my post, other posts at tht url suggest your syntax should work as is.


RE: Python, how to input a complex number - Dirk.nl - 04-29-2021 05:04 PM

Hello Stevetuc,
Thank you for your answer. I have also tried this in many ways. Also to use “complex ()” separately. Also tried with “from builtins import *”.
Always the same error message.
If you read the help info at “complex” we talk about that if the first parameter is a string, the second should not be a string ??. Input returns a complete string.
I don't really get any wiser from the help info.
Maybe Cyrille can tell me (us) about this, if he has the time !


RE: Python, how to input a complex number - Didier Lachieze - 04-29-2021 06:37 PM

(04-29-2021 02:36 PM)Dirk.nl Wrote:  According to Python documents, you can enter a complex number like this; var=complex(input())
For example, 3+5j
The Python App gives an error message here.
Could someone for me test / try this and explain what I'm doing wrong ?

I tested it on the Numworks virtual app and got also an error message, this may be a limitation of Micropython.

[Image: mini_210429084530470275.png] [Image: mini_210429083642969630.png] [Image: mini_210429084529667009.png]


RE: Python, how to input a complex number - Dirk.nl - 04-29-2021 07:20 PM

Didier,
That's strange, a cmath lib to be able to calculate with complex numbers, but these cannot be entered!
Probably to enter some other way, maybe entering real and imaginary parts separately and merge afterwards?
I don't know how yet !!! Still have something to find out.
Regards, Dirk


RE: Python, how to input a complex number - Stevetuc - 04-30-2021 08:55 AM

(04-29-2021 06:37 PM)Didier Lachieze Wrote:  
(04-29-2021 02:36 PM)Dirk.nl Wrote:  According to Python documents, you can enter a complex number like this; var=complex(input())
For example, 3+5j
The Python App gives an error message here.
Could someone for me test / try this and explain what I'm doing wrong ?

I tested it on the Numworks virtual app and got also an error message, this may be a limitation of Micropython.

[Image: mini_210429084530470275.png] [Image: mini_210429083642969630.png] [Image: mini_210429084529667009.png]

Complex() should accept either a string or a number as input.
https://www.google.com/amp/s/www.learnbyexample.org/python-complex-function/amp/
On the Numworks, it only accepts a number.
Code:

x=5+1j;
x=complex(x);  #this works
x=complex(5,1) #this works

x="5+1j"
x=complex(x) #this doesnt
Since input() returns a string, this also doesn't work.


RE: Python, how to input a complex number - Dirk.nl - 04-30-2021 09:51 AM

Hi Stevetuc, thanks

I discovered this yesterday for the HP Prime as well.
Indeed, <complex> does not work with a string, contrary to what the help info indicates.
I think this could indeed be a bug in micropython.
To test, I solved this as follows:

PHP Code:
a=float(input(“real:))
print(
a)
b=float(input(“imag:))
print(
b)
c=complex(a,b)
print(
c

Regards


RE: Python, how to input a complex number - toml_12953 - 04-30-2021 10:36 AM

(04-29-2021 02:36 PM)Dirk.nl Wrote:  According to Python documents, you can enter a complex number like this; var=complex(input())
For example, 3+5j
The Python App gives an error message here.
Could someone for me test / try this and explain what I'm doing wrong ?

How about var=complex(float(input()))


RE: Python, how to input a complex number - Albert Chan - 04-30-2021 10:36 AM

(04-30-2021 08:55 AM)Stevetuc Wrote:  Since input() returns a string, this also doesn't work.

"Old" python used to return evaluated expression, instead of string.

input(s) ≡ eval(raw_input(s))

Have you tried using eval, instead of complex ?


RE: Python, how to input a complex number - Dirk.nl - 04-30-2021 10:55 AM

Albert,

GREAT ! Works with eval instead of complex.
Thank you very very much for your answer!

Regards, Dirk


RE: Python, how to input a complex number - parisse - 04-30-2021 11:57 AM

I checked and this is not specific to the Prime, but a limitation of MicroPython.


RE: Python, how to input a complex number - Dirk.nl - 04-30-2021 01:21 PM

Thanks Bernard,

Indeed, this is not due to the Prime.
Since I'm trying to understand something about MicroPython I didn't know about <eval>, it's nice that Albert Chan gave me that tip. I have a little more experience with PL/M 86 (long time ago), SCL Siemens S7, C++ and recently HPPL.

Regards, Dirk