【问题标题】:employee management syntax errors [closed]员工管理语法错误[关闭]
【发布时间】:2022-01-07 13:29:21
【问题描述】:

我正在创建一个代码来为一家虚构的纸业公司制作部门员工名单。我已经弄清楚了大部分代码,但是我遇到了很多我不确定如何修复的错误。我正在用 python 编码并使用 pycharm。建议/帮助将不胜感激:)

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 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 = p[]
    for employee in listOfEmployees:
        if employee.position == "Manager" or   "Manager" in employee.position
            managementList.append(employee)
        elif employee.position == "Salesperson" or "Salesperson" in employee.position:
            salesList.append(employee)
        elif employee.position == "Accountant" or "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 = 0
        for emp1 in dep:
            if emp1.age > max:
                max = emp1.age
        for emp1 in dep:
            if max == 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"))

在我确定我必须将某些表单 listOfEmployees 更改为 self 但特别是第 14 行 pycharm 在“listOfEmployees”下给了我一条红线,所以我不确定该怎么做,因为它要求我创建参数或重命名参考,我确定我确实创建了一个参数?我不是 1000% 确定。这是针对课堂上的一个项目,我正在尽力而为。感谢大家对这个 T__T 的耐心等待

【问题讨论】:

  • 应该是`对于listOfEmployees中的项目:`而且你的缩进似乎是错误的,至少在这里的问题中前两行是相同级别的缩进,这是不可能的!
  • 哎呀!哦,我的上帝,非常感谢@JeffUK

标签: python python-3.x pycharm


【解决方案1】:

您的代码存在很多缩进问题。请记住,Python 对此非常挑剔。我主要必须解决缩进问题和一些可能只是拼写错误的琐碎问题,因为您似乎了解底层代码。当你看到一条红线时,试着悬停它,看看 Pycharm 想要告诉你什么。你会犯很多这样的错误,所以值得学习如何使用你的工具来找出这些错误。

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 employee.position == "Manager" or   "Manager" in employee.position:
            managementList.append(employee)
        elif employee.position == "Salesperson" or "Salesperson" in employee.position:
            salesList.append(employee)
        elif employee.position == "Accountant" or "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 = 0
        for emp1 in dep:
            if emp1.age > max:
                max = emp1.age
        for emp1 in dep:
            if max == 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 2018-09-23
    • 2014-02-12
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多