You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python员工管理系统代码语法错误及PyCharm报错修复求助

代码问题修复说明

你不需要将listOfEmployees替换为self,当前你编写的员工处理函数均为独立工具函数,不属于employee类的成员方法,无需self参数。第14行PyCharm标红是语法错误导致的IDE识别异常,并非参数未定义,修复对应语法问题后标红会自动消失。

所有语法错误修复点

  • 类方法缩进错误:__init__是employee类的成员,需要缩进4个空格
  • 第14行for循环语法错误:缺少in关键字,修正为for item in listOfEmployees:
  • allocate_department函数中accountantList初始化错误:多写了无效字符p,修正为accountantList = []
  • allocate_department函数中if判断语句末尾缺少冒号,原有==判断冗余可直接简化为in判断,修正为if "Manager" in employee.position:
  • display_department_employees函数整体缩进错误:函数体所有代码需要缩进4个空格,if/elif/else分支下的打印语句需要额外再缩进一层
  • display_department_employees函数中遍历部门员工的for循环缩进错误:需要放在遍历部门的外层循环内部
  • 员工列表初始化错误:你将employeeList定义为元组(),元组没有append方法,修正为列表employeeList = []
  • head_of_department函数中变量名冲突:不要用内置函数名max作为变量名,替换为max_age避免覆盖内置功能
  • 职位前缀优化:给Head后缀加空格,避免出现HeadManager这类无空格的不友好显示

修复后完整代码

class employee:
    def __init__(self, name, age, position) -> None:
        """Initializes all employees with name, age, and position attributes"""
        self.name = name
        self.age = age
        self.position = position


def display_employees(listOfEmployees):
    """Displays all employees in the company"""
    print()
    print("Employees in Dunder Mifflin are: ")
    print()
    for item in listOfEmployees:
        print(item.name + ",", str(item.age) + ",", item.position)

def allocate_department(listOfEmployees):
    """Sorts all employees to departments and returns a list of departments"""
    allocatedList = []
    managementList = []
    salesList = []
    accountantList = []
    for employee in listOfEmployees:
        if "Manager" in employee.position:
            managementList.append(employee)
        elif "Salesperson" in employee.position:
            salesList.append(employee)
        elif "Accountant" in employee.position:
            accountantList.append(employee)
        else:
            pass
    allocatedList.append(managementList)
    allocatedList.append(salesList)
    allocatedList.append(accountantList)
    return allocatedList


def display_department_employees(listOfDep):
    """Displays employees in their departments"""
    print()
    print("The individual employees in each department shown below:")
    print("---------------------------------------")
    print()
    n = 1
    for dep in listOfDep:
        if n == 1:
            print("Management:")
            print("-----------")
        elif n==2:
            print("Salesperson:")
            print("-----------")
        else:
            print("Accountants")
            print("-----------")
        for emp1 in dep:
            print(emp1.name + ",", str(emp1.age) + ",", emp1.position)
        print()
        n += 1

def head_of_department(listOfDep):
    """Finds max age and then sets employee as head"""
    for dep in listOfDep:
        max_age = 0
        for emp1 in dep:
            if emp1.age > max_age:
                max_age = emp1.age
        for emp1 in dep:
            if max_age == emp1.age:
                emp1.position = "Head " + emp1.position


employeeList = []
employeeList.append(employee("Michael", 45, "Manager"))
employeeList.append(employee("Dwight", 40, "Assistant to the Manager"))
employeeList.append(employee("Jim", 35, "Manager"))
employeeList.append(employee("Pam", 30, "Receptionist"))
employeeList.append(employee("Angela", 32, "Accountant"))
employeeList.append(employee("Kevin", 42, "Accountant"))
employeeList.append(employee("Stanley", 55, "Salesperson"))
employeeList.append(employee("Andy", 38, "Salesperson"))
employeeList.append(employee("Ryan", 30, "Salesperson"))
employeeList.append(employee("Creed", 55, "Salesperson"))

# 测试代码,可直接运行查看效果
display_employees(employeeList)
dep_list = allocate_department(employeeList)
head_of_department(dep_list)
display_department_employees(dep_list)

内容的提问来源于stack exchange,提问作者MoniqueTrying

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 02:57:01