Hello, I am currently trying to learn programming and am using Python to start with. I am currently practicing programming with classes and have placed the following code into a python file called complex.py:

class Complex
        def __init__(self, realpart, imagpart):
            self.r = realpart
            self.i = imagpart

and placed the following code into another file named main.py in the same exact folder:

import complex

x = Complex( 3.0, -4.5)
print "x's Real component %s" % x.r
print "x's Imaginary component %s" % x.i

When I ran main.py through the compiler I was given a message claiming that there was no module named Complex. Is there something I did wrong when inputting it?

And by the way, when I added the class definition at the top of the main.py instead of through a whole new file the code compiled perfectly fine.

Recommended Answers

All 2 Replies

You imported complex but did not use it. You must include the module name:

complex.Complex(3.0, -4.5)

By the way Python has complex numbers like 3.0-4.5j (enginering style j instead of i for imaginary unit)

Also class Complex needs a :
Please make your indentations uniform with 4 spaces being the most common way. Any good Python IDE will do that for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.