HP Forums

Full Version: Python, how to input a complex number
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 ?
(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.
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 !
(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]
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
(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.learnby...ction/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.
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
(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()))
(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 ?
Albert,

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

Regards, Dirk
I checked and this is not specific to the Prime, but a limitation of MicroPython.
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
Reference URL's