Python Forum

Full Version: SyntaxError: invalid syntax
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm practising developing classes and wrote this code.

class Employee:
    
	raise_amt = 1.04
	
	def __init__(self, first, last, pay):
	    self.first = first
	    self.last = last
	    self.email = first + '.' + last + '@email.com'
	    self.pay = pay
	
	def fullname(self):
	    return '{} {}'.format(self.first, self.last)
		
	def apply_raise(self):
	    self.pay = int(self.pay * self.raise_amt)
	
class Developer(Employee):
    raise_amt = 1.1
    def __init__(self, first, last, pay, prog_lang):
        super().__init__(first, last, pay )
        self.prog_lang = prog_lang

class Manager(Employee):
    def __init__(self, first, last, pay, employees=None):
        super().__init__(first, last, pay )
        self.prog_lang = prog_lang
        if employees is None:
            self.employees = []
        else:
            self.employees = employees
    
    def add_emp(self, emp):
        if emp not in self.employees:
            self.employees.append(emp)
    def remove_emp(self, emp):
        if emp in self.employees:
            self.employees.remove(emp)
    def print_emps(self):
        for emp in self.employees:
            print('-->', emp(fullname())

dev_1 = Developer('Corey', 'Schafer', 50000, 'Python')
dev_2 = Developer('Test', 'Employee', 60000, 'Java')
                  
mgr_1 = Manager('Sue', 'Smith', 90000, [dev_1])
print(mgr_1(email))

print(dev_1.pay)
dev_1.apply_raise()
print(dev_1.pay)
print(dev_1.prog_lang)
dev_1 = Developer('Corey', 'Schafer', 50000, 'Python')
^
SyntaxError: invalid syntax


I don't see the reason for this syntax error. Please advise.
Count parentheses in line before error.
Thank you. An another error showed up

self.prog_lang = prog_lang
NameError: name 'prog_lang' is not defined
You don't say which line caused the error. If it was from line 26 (which I suspect it was) then the problem is that you don't define a prog_lang parameter for the __init__ method of Manager. Maybe you copied the __init__ method from Developer?
Correct, I added it.
But it looks that there is no end to errors. Now for
print('-->', emp(fullname()))
:
NameError: name 'fullname' is not defined

I defined fullname in Employee class. Not sure how to print emps.
There are several error and logic that don't work(like add,remove).
Here fixing so most run,the logic you have to look at again.
Do small step,and test your code at each step,than some of this mess could be avoided Wink
class Employee:
    raise_amt = 1.04
    def __init__(self, first, last, pay):
        self.first = first
        self.last = last
        self.email = '{}.{}@email.com'.format(first, last)
        self.pay = pay

    @property
    def fullname(self):
        return '{} {}'.format(self.first, self.last)

    @property
    def apply_raise(self):
        self.pay = int(self.pay * self.raise_amt)

class Developer(Employee):
    raise_amt = 1.1
    def __init__(self, first, last, pay, prog_lang):
        super().__init__(first, last, pay)
        self.prog_lang = prog_lang

class Manager(Employee):
    def __init__(self, first, last, pay, employees=None):
        super().__init__(first, last, pay)
        if employees is None:
            self.employees = []
        else:
            self.employees = employees

    def add_emp(self, emp):
        '''
        if emp not in self.employees:
            self.employees.append(emp)'''

    def remove_emp(self, foo):
        for emp in self.employees:
            if emp.last == foo:
                # Fix so it print something
                print(emp.last)

    def print_emps(self):
        for emp in self.employees:
            print('--> {} {}'.format(emp.first, emp.last))

dev_1 = Developer('Corey', 'Schafer', 50000, 'Python')
dev_2 = Developer('Test', 'Employee', 60000, 'Java')

mgr_1 = Manager('Sue', 'Smith', 90000, [dev_1])
Test:
>>> dev_1.fullname
'Corey Schafer'
>>> dev_1.prog_lang
'Python'
>>> dev_1.pay
50000
>>> dev_1.apply_raise
>>> dev_1.pay
55000
>>> mgr_1.print_emps()
--> Corey Schafer
>>> mgr_1.remove_emp('Schafer')
Schafer
The way to be successful at coding programs of any size is to write and test one concept (function, or method) at a time,
and to keep them short and for single purpose.
Takes much less time than writing the entire program and then trying to debug all at once,
the latter is often the reason for failure.

Just as snippsat says:
Quote:Do small step,and test your code at each step,than some of this mess could be avoided
many thanks for the explanations - this is a great thread which treats the importance of class-variables and shows how to use them.

many thanks !!!