Python Forum

Full Version: Python Compile error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have simple program running on pyscripter 2.5 and similar code tried on jupyter notebook.
How to resolve below error

reference code taken from https://codeigo.com/python/import-class-...-directory

car.py
class Car:
    def init(self, company, model, year):
        self.company = company
        self.model = model
        self.year = year
    def get_details(self):
        details = str(self.year) + ' ' + self.company + ' ' + self.model
        return details
main.py
from car import Car
print(Car)
print('enter the code')
mycar = Car('Ford', 'Escort', 2000)
error
Output:
<class 'car.Car'> enter the code Traceback (most recent call last): File "C:\Users\python\main.py", line 5, in <module> mycar = Car('Ford', 'Escort', 2000) TypeError: Car() takes no arguments
note, it should be __init__, not init
Thanks It worked. I have similar example
complexnumber.py
class Complex:
    def __init__(self,real,imaginary):
        self.real=real
        self.imaginary=imaginary   
    def __add__(self,right):
        return Complex(self.real+right.real,self.imaginary+right.imaginary)
    def __iadd__(self,right):
        self.real+=right.real
        self.imaginary+=right.imaginary
        return self
    def __repr__(self):
        return (f'({self.real}'+('+' if self.imaginary>=0 else '-')+
               f'{abs(self.imaginary)}i)')
        
But when try using ipython module i get error. But it work on pyscripter
Output:
from complexnumber import complex --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-7-470da91856be> in <module> ----> 1 from complexnumber import complex ~\Pictures\Pythoncode\Class\complexnumber.py in <module> 52 { 53 "cell_type": "code", ---> 54 "execution_count": null, 55 "id": "dc586128-a712-4774-9f79-6d36de5408b3", 56 "metadata": {}, NameError: name 'null' is not defined
You are not showing the code where the error occurs. Can you paste more (all) of your code?
(Jun-03-2021, 10:16 AM)ajitnayak1987 Wrote: [ -> ]
Output:
from complexnumber import complex--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-7-470da91856be> in <module> ----> 1 from complexnumber import complex
The name of the class is "Complex". Not "complex". Remember Python is case sensitive.