在Python中查找并替换子列表

时间:2020-02-19 04:32:36

标签: python input replace sublist

我一般对Python和程序设计还很陌生,遇到了一种我无法弄清的情况。

我正在构建一个员工管理系统,用户可以在其中添加新员工,查看所有员工,通过SSN搜索员工,并通过搜索SSN并重新输入所需的输入来替换员工以前的信息。最后一部分是我失败的地方。我相信我可以通过SSN进行适当的搜索,但是我不知道如何让用户替换该子列表中的先前信息。谁能帮我解决这个问题?我还在脚本下面包含了我收到的错误消息。

employee_info = [ ]

while True: # While loop created to make the script constantly run
    counter = len(employee_info) # Counter created so that the user will know how many entries there are

    print('There are', '(', (int(counter)), ')', 'employees in the system.\n')

    add_new = input('Would you like to add a new employee to the system, find an employee\'s record by SSN, change an employee\'s information or view all employee entries in the system?\
    To add an employee, type: "ADD". To view all employees currently in the system, type: "VIEW ALL EMPLOYEES." To find an employee, type: "SEARCH EMPLOYEE BY SSN." To change employee info, type: "CHANGE."\n'\
    ) #Added option to add an employee, find an employee by SSN, change information or to view all employees currently the system

    if add_new == 'ADD':
        while True: # Loop created to input employees with option to quit
            employee_index = [input('Employee Name\n'), input('Employee SSN\n'), \
            input('Employee Telephone Number ***Entered as (555)555-5555*** \n'), input('Employee Email\n'), input('Employee Salary ***Entered as $xxxx***\n')] 
            employee_info.append(employee_index)
            more_employees = input('Would you like to add another employee? Y or N.\n')
            if more_employees == 'Y':
                continue
            elif more_employees == 'N':
                break

    elif add_new == 'VIEW':
        for employee_index in employee_info:
            print('            -----------------', employee_index[0], '-----------------\n')
            print('SSN:', employee_index[1], '\n')
            print('Phone:', '(' + employee_index[2][0] + employee_index[2][1] + employee_index[2][2] + ')' + employee_index[2][3] + employee_index[2][4] + employee_index[2][
            5] + '-' + employee_index[2][6] + employee_index[2][7] + employee_index[2][8] + employee_index[2][9], '\n') 
            print('Email:', employee_index[3], '\n')
            print('Salary:', '$' + str(employee_index[4]), '\n')
            print('            ----------------------------------------------------')

    elif add_new == "SEARCH":
        find_emp = input('Please enter the employee SSN in the following format: 333221111.\n')
        found = False
        for employee in employee_info:
            if employee[1] == find_emp:
                print('            -----------------', employee[0], '-----------------\n')
                print('SSN:', employee[1], '\n')
                print('Phone:', '(' + employee[2][0] + employee[2][1] + employee[2][2] + ')' + employee[2][3] + employee[2][4] + employee[2][
                5] + '-' + employee[2][6] + employee[2][7] + employee[2][8] + employee[2][9], '\n') 
                print('Email:', employee[3], '\n')
                print('Salary:', '$' + str(employee[4]), '\n')
                print('            ----------------------------------------------------')
                found = True

        if found == False:
            print("Employee not found!")
    elif add_new == "CHANGE":
        find_emp = input('Please enter the employee SSN in the following format: 333221111.\n')
        found = False
        for employee in employee_info:
            if employee[1] == find_emp:
                changeName = input(employee.replace(employee[0], 'Please enter new name.\n'))
                changeSSN = input(employee.replace(employee[1], 'Please enter new SSN.\n'))
                changePhone = input(employee.replace(employee[2], 'Please enter new phone number.\n'))
                changeEmail = input(employee.replace(employee[3], 'Please enter new email.\n'))
                changeSalary = input(employee.replace(employee[4], 'Please enter new salary.\n'))

                found = True

        if found == False:
            print("Employee not found!")

Traceback (most recent call last):
  File "C:\Users\jmcra\Desktop\CPT200\Employment Management System - Functionality (4).py", line 54, in <module>
    changeName = input(employee.replace(employee[0], 'Please enter new name.\n'))
AttributeError: 'list' object has no attribute 'replace'

1 个答案:

答案 0 :(得分:0)

列表没有方法“替换” 替换您的列表,请改用此

employee[0] = input('New name')

这是您的代码

elif add_new == "CHANGE":
    find_emp = input('Please enter the employee SSN in the following format: 333221111.\n')
    found = False
    for employee in employee_info:
        if employee[1] == find_emp:
            changeName = input('Please enter new name.\n')
            changeSSN = input('Please enter new SSN.\n')
            changePhone = input('Please enter new phone number.\n')
            changeEmail = input('Please enter new email.\n')
            changeSalary = input('Please enter new salary.\n')

            employee[0] = changeName;
            employee[1] = changeSSN;
            employee[2] = changePhone;
            employee[3] = changeEmail;
            employee[4] = changeSalary;


            found = True

您可以在此处查看所有列表方法: https://www.programiz.com/python-programming/methods/list

相关问题