Post Reply 
Python, how to input a complex number
04-29-2021, 02:36 PM
Post: #1
Python, how to input a complex number
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 ?

— Dirk Hartland
Find all posts by this user
Quote this message in a reply
04-29-2021, 04:02 PM (This post was last modified: 04-29-2021 04:13 PM by Stevetuc.)
Post: #2
RE: Python, how to input a complex number
(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.
Find all posts by this user
Quote this message in a reply
04-29-2021, 05:04 PM
Post: #3
RE: Python, how to input a complex number
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 !

— Dirk Hartland
Find all posts by this user
Quote this message in a reply
04-29-2021, 06:37 PM
Post: #4
RE: Python, how to input a complex number
(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]
Find all posts by this user
Quote this message in a reply
04-29-2021, 07:20 PM
Post: #5
RE: Python, how to input a complex number
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

— Dirk Hartland
Find all posts by this user
Quote this message in a reply
04-30-2021, 08:55 AM (This post was last modified: 04-30-2021 08:58 AM by Stevetuc.)
Post: #6
RE: Python, how to input a complex number
(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.
Find all posts by this user
Quote this message in a reply
04-30-2021, 09:51 AM
Post: #7
RE: Python, how to input a complex number
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

— Dirk Hartland
Find all posts by this user
Quote this message in a reply
04-30-2021, 10:36 AM
Post: #8
RE: Python, how to input a complex number
(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()))

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
04-30-2021, 10:36 AM
Post: #9
RE: Python, how to input a complex number
(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 ?
Find all posts by this user
Quote this message in a reply
04-30-2021, 10:55 AM
Post: #10
RE: Python, how to input a complex number
Albert,

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

Regards, Dirk

— Dirk Hartland
Find all posts by this user
Quote this message in a reply
04-30-2021, 11:57 AM
Post: #11
RE: Python, how to input a complex number
I checked and this is not specific to the Prime, but a limitation of MicroPython.
Find all posts by this user
Quote this message in a reply
04-30-2021, 01:21 PM
Post: #12
RE: Python, how to input a complex number
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

— Dirk Hartland
Find all posts by this user
Quote this message in a reply
Post Reply 




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