在Python中查找并替换子列表

在Python中查找并替换子列表,python,input,replace,sublist,Python,Input,Replace,Sublist,一般来说,我对Python和编程相当陌生,遇到了一种我无法理解的情况 我正在构建一个员工管理系统,用户可以在其中添加新员工、查看所有员工、通过SSN搜索员工,并通过搜索SSN和重新输入所需输入来替换员工以前的信息。最后一部分是我失败的地方。我相信我能够通过SSN进行适当的搜索,但我不知道如何让用户替换该子列表中以前的信息。有人能帮我弄清楚吗?我还将收到的错误消息包含在脚本下面 employee_info = [ ] while True: # While loop created to mak

一般来说,我对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'
employee_info=[]
while True:#创建while循环以使脚本持续运行
counter=len(员工信息)#创建计数器,以便用户知道有多少条条目
打印('系统中有','(',(int(计数器)),'),'员工。\n')
add_new=input('您想向系统中添加新员工、通过SSN查找员工记录、更改员工信息还是查看系统中的所有员工条目\
若要添加员工,请键入“添加”。若要查看系统中当前的所有员工,请键入“查看所有员工”。若要查找员工,请键入“按SSN搜索员工”。若要更改员工信息,请键入:“更改”。\n\
)#添加了添加员工、通过SSN查找员工、更改信息或查看系统当前所有员工的选项
如果add_new==“add”:
如果为True:#创建循环以输入员工,并选择退出
员工索引=[输入('employee Name\n'),输入('employee SSN\n')\
输入(“员工电话号码***输入为(555)555-5555***\n”)、输入(“员工电子邮件\n”)、输入(“员工工资***输入为$xxxx***\n”)]
员工信息附加(员工索引)
more_employees=input('您想添加另一名员工吗?Y还是N.\N')
如果更多的_员工==“Y”:
持续
elif more_employees==“N”:
打破
elif add_new==“查看”:
对于员工信息中的员工索引:
打印('--------------',员工索引[0],'--------------\n')
打印('SSN:',员工索引[1],'\n')
打印('Phone:','('+雇员指数[2][0]+雇员指数[2][1]+雇员指数[2][2]+)'+雇员指数[2][3]+雇员指数[2][4]+雇员指数[2][
5] +'-'+员工指数[2][6]+员工指数[2][7]+员工指数[2][8]+员工指数[2][9],'\n')
打印('电子邮件:',员工索引[3],'\n')
打印('Salary:','$'+str(员工索引[4]),'\n')
打印(“-------------------------------------------------------------”)
elif add_new==“搜索”:
find_emp=input('请按以下格式输入员工SSN:333221111。\n')
发现=错误
对于员工信息中的员工:
如果员工[1]==查找环境管理计划:
打印('--------------',员工[0],'--------------\n')
打印('SSN:',员工[1],'\n')
打印('电话:','('+员工[2][0]+员工[2][1]+员工[2][2]+)'+员工[2][3]+员工[2][4]+员工[2][
5] +'-'+雇员[2][6]+雇员[2][7]+雇员[2][8]+雇员[2][9],'\n')
打印('电子邮件:',员工[3],'\n')
打印('Salary:'、'$'+str(员工[4])、'\n')
打印(“-------------------------------------------------------------”)
找到=真
如果发现==False:
打印(“未找到员工!”)
elif add_new==“CHANGE”:
find_emp=input('请按以下格式输入员工SSN:333221111。\n')
发现=错误
对于员工信息中的员工:
如果员工[1]==查找环境管理计划:
changeName=input(employee.replace(employee[0],'请输入新名称。\n'))
changeSSN=input(employee.replace(employee[1],'请输入新的SSN.\n'))
changePhone=input(employee.replace(employee[2],'请输入新电话号码。\n'))
changeEmail=输入(employee.replace(employee[3],“请输入新电子邮件。\n”))
changeSalary=输入(employee.replace(employee[4],“请输入新工资。\n”))
找到=真
如果发现==False:
打印(“未找到员工!”)
回溯(最近一次呼叫最后一次):
文件“C:\Users\jmcra\Desktop\CPT200\Employment Management System-Functionality(4.py)”,第54行,在
changeName=input(employee.replace(employee[0],'请输入新名称。\n'))
AttributeError:“列表”对象没有属性“替换”
列表没有“替换”方法 要替换您的列表,请使用此选项

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
您可以在此处查看所有列表方法: