SyntaxError: invalid syntax - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: SyntaxError: invalid syntax (/thread-9087.html) |
SyntaxError: invalid syntax - Truman - Mar-20-2018 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. RE: SyntaxError: invalid syntax - snippsat - Mar-20-2018 Count parentheses in line before error. RE: SyntaxError: invalid syntax - Truman - Mar-20-2018 Thank you. An another error showed up self.prog_lang = prog_lang NameError: name 'prog_lang' is not defined RE: SyntaxError: invalid syntax - techwriter13 - Mar-20-2018 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? RE: SyntaxError: invalid syntax - Truman - Mar-20-2018 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. RE: SyntaxError: invalid syntax - snippsat - Mar-20-2018 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 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 RE: SyntaxError: invalid syntax - Larz60+ - Mar-21-2018 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 RE: SyntaxError: invalid syntax - apollo - Jun-16-2019 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 !!! |