Python core Programming chapter sixth Exercise 6-13

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.