6-13.
Strings. The string module contains three functions, atoi (), Atol (), and Atof (), each of which is responsible for converting the string into integer, long, and floating-point numbers. From Python 1.5, Python's built-in function int (), long (), and float () can do the same thing, and this article, the complex () function can convert strings into complex numbers (however, before 1.5, these conversion functions can only work on numbers) from the blog park.
The string module does not implement a ATOC () function, then you implement a ATOC (), accept a single string as a parameter input, a string representing a complex number, such as ' -1.23e+4-5.67j ', returning the corresponding complex object. You can't use the eval () function, but you can use the complex () function, and you only have to use the following restrictions: Complex (): Complex (real, imag) both real and imag must be floating-point values.
Answer
The code is as follows:
def atoc (input): real = complex (input). Real imag = complex (input). imag print ' Complex Real is: ', real print ' Complex Imag is: ', imag input = raw_input (' Please input a complex number: ') ATOC (input)
Python core Programming chapter sixth Exercise 6-13