简体   繁体   English

如何创建二维列表,并从此列表中提取每个输入数据?

[英]How do I create a 2-d list, and pull each of the input data from this list?

A bit of context, the user enters employee name, hourly wage, and hours worked.结合上下文,用户输入员工姓名、小时工资和工作时间。 I am suppose to format all of this data into a 2-d list, and then find the payroll for each employee based off the entered wage and hours worked.我想将所有这些数据格式化为一个二维列表,然后根据输入的工资和工作时间找到每个员工的工资单。 Program is then suppose to output employee name and how much they are suppose to be paid.然后程序假设 output 员工姓名以及他们应该支付多少。 Not sure how to turn variable info into a 2-d list.不确定如何将变量信息转换为二维列表。 User is to input data as "name, hourly wage, hours worked", so for example "John, 15, 35"用户输入数据为“姓名、小时工资、工作时间”,例如“John, 15, 35”

def main():
    payroll = 0
    employee_list = []
    new_list = []
    print('Payroll for the Global Thingamabob Manufacturing Company')
    print('Enter Information ( Name, hours worked, hourly rate) seperated by commas')
    print('Press Enter to stop Enetering Employee Information')
    info = input('Enter Employee Information: ')
    
    while info != '':
        employee_list = info.split()
        info = input('Enter Employee Information: ')
        
    print()
    print('Total Payroll ${:.2f}'.format(payroll))
    print()
    print('Employees with Paychecks')

main()

A few of problems with your code:您的代码存在一些问题:

  1. You overwrite employee_list every time you receive an input.每次收到输入时都会覆盖employee_list Instead, you should append the new information to employee_list相反,您应该将新信息append 添加employee_list
  2. You asked for a comma-separated input but you split on whitespace.您要求以逗号分隔的输入,但您以空格分隔。 Do .split(',') instead.改为执行.split(',')
  3. You never calculate payroll .你永远不会计算payroll Is it meant to be the sum of hourly_rate * hours_worked for all employees?这是否意味着所有员工的hourly_rate * hours_worked之和?
    ...
    employees_list = [] # A list to hold all employees information
    payroll = 0         # Initialize payroll to zero so you can add to it as you process each employee
    info = input('Enter Employee Information: ')
    
    while info != '':
        employee_info = info.split(',')
        if len(employee_info) != 3:
            print("Please enter Name, hours worked, hourly rate")
        else:
            try:
                hours_worked = float(employee_info[1]) # Convert hours worked to float
                hourly_rate = float(employee_info[2]) # Convert hourly rate
                employee_name = employee_info[0].strip() # Remove spaces before and after name
                employee_info = [employee_name, hours_worked, hourly_rate] # Create a list with name, numeric hours and rate
                employees_list.append(employee_info) # Append info list to list of all employees

                # Here, you would calculate the amount owed to the employee
                # and add it to payroll, but that's simple enough so you can figure it out.

            except ValueError: # Conversion to float failed for either hours or rate
                print("Please enter numeric values for hours worked and hourly rate")

        info = input('Enter Employee Information: ')

    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM