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 |
|||
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())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:
Edit: hmm ignore my post, other posts at tht url suggest your syntax should work as is. |
|||
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 |
|||
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()) I tested it on the Numworks virtual app and got also an error message, this may be a limitation of Micropython. |
|||
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 |
|||
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()) 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:
|
|||
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:“)) Regards — Dirk Hartland |
|||
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()) How about var=complex(float(input())) Tom L Cui bono? |
|||
04-30-2021, 10:36 AM
Post: #9
|
|||
|
|||
RE: Python, how to input a complex number | |||
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 |
|||
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.
|
|||
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 |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)