0

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. 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"

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()
jcroll
  • 21
  • 2
  • 1
    Please update your post with a sample out of what you expect given an input. – Simon Apr 19 '22 at 17:27
  • 1
    this: `employee_list = info.split()` just keeps overwriting the variable with a new list created by `info.split()`... did you mean to use `.append`? – juanpa.arrivillaga Apr 19 '22 at 17:34
  • You might also consider making a [dataclass](https://docs.python.org/3/library/dataclasses.html) of `Employee` for example with 3 members `name`, `wage`, `hours` then have a list of Employee – Cory Kramer Apr 19 '22 at 17:35

1 Answers1

0

A few of problems with your code:

  1. You overwrite employee_list every time you receive an input. Instead, you should append the new information to employee_list
  2. You asked for a comma-separated input but you split on whitespace. Do .split(',') instead.
  3. You never calculate payroll. Is it meant to be the sum of hourly_rate * hours_worked for all employees?
    ...
    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: ')

    ...
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • This worked great, the only issue is when I'm trying to output the employee name with payroll it continuously outputs with brackets. I need it to say "John $300", except is outputs [John, 300]. – jcroll Apr 19 '22 at 23:02
  • @jcroll see https://stackoverflow.com/questions/39358742/converting-list-of-lists-into-a-table – Pranav Hosangadi Apr 20 '22 at 15:13