Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SyntaxError: invalid syntax
#1
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.
Reply
#2
Count parentheses in line before error.
Reply
#3
Thank you. An another error showed up

self.prog_lang = prog_lang
NameError: name 'prog_lang' is not defined
Reply
#4
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?
Reply
#5
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.
Reply
#6
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
Reply
#7
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
Reply
#8
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 !!!
Wordpress - super toolkits a. http://wpgear.org/ :: und b. https://github.com/miziomon/awesome-wordpress :: Awesome WordPress: A curated list of amazingly awesome WordPress resources and awesome python things https://github.com/vinta/awesome-python
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print(data) is suddenly invalid syntax db042190 6 1,119 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  SyntaxError: invalid syntax ?? korenron 15 5,567 Jan-25-2022, 11:46 AM
Last Post: korenron
  Invalid syntax with an f-string Mark17 7 7,555 Jan-14-2022, 04:44 PM
Last Post: Mark17
  invalid syntax in my class CompleteNewb 2 1,844 Dec-13-2021, 09:39 AM
Last Post: Larz60+
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 3,092 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Unexplained Invalid syntax Error cybertooth 5 3,174 Aug-02-2021, 10:05 AM
Last Post: cybertooth
  [split] SyntaxError: invalid syntax Code_X 3 2,702 May-04-2021, 05:15 PM
Last Post: Yoriz
  Invalid syntax error - need help fixing calgk01 3 3,225 Feb-23-2021, 08:41 PM
Last Post: nilamo
  Invalid syntax using conditionals if - else jperezqu 1 2,295 Jan-13-2021, 07:32 PM
Last Post: bowlofred
  invalid syntax in line 5. Help Asadzangibaloch 2 2,348 Dec-10-2020, 04:26 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020