0

I'm trying to create a function that takes two prmtrs, num_of_employees and hrly_rate.

The list should consist of the number of worked hours and the pay of each employee. The size of the list should be the same as the number of employees. It prompts the user to enter the number of hours worked for each employee, calculates the pay, and creates a list of the number of hours and pay that adds it to the list.

If the number is less than 40, the pay is standard: Number of hours worked * Hourly Rate If the number is GREATER than 40, then the pay is the hourly rate is 1.5 for the hours over 40 hours.

The Code SHOULD Display this output: #assuming the number of employees is just 2,:

insert the number of worked hours: 60
insert the number of worked hours:20
the employee worked for 60 hours and the earned is 700.00$
the employee worked for 20 hours and the earned is 200.00$

But this is my Code:

def calculate_total_pay_cad(num_employees, hrly_rate_CAD):
    rng = range(0, num_employees)
    for n in rng:
        num_hrs_worked = int(input(('Input the number of worked hours: ')))
    if num_hrs_worked < 40:
        pay = num_hrs_worked * hrly_rate_CAD
    if num_hrs_worked > 40:
        pay = num_hrs_worked * (hrly_rate_CAD * 1.5)
    for n in rng:
        print(f"the employee worked for {num_hrs_worked} and has earned {pay}")

num_employees = int(input(('enter number employees')))
hrly_rate_CAD = int(input(('enter hourly rate: ')))

calculate_total_pay_cad(num_employees,hrly_rate_CAD)

And this is my Output:

 Input the number of worked hours: 50
Input the number of worked hours: 20
the employee worked for 20 and has earned 300
the employee worked for 20 and has earned 300

Summary = My code is only outputting the LAST insert on my list of inputs. How would I make it accompany the correct number of hours and pay calculation for each individual line?

Many thanks in advance.

1 Answer 1

1

This is because your calculations are done outside of the for loop. This means it will only use the last value entered for all calculations. Instead, move everything inside the loop since you would like an output per employee:

def calculate_total_pay_cad(num_employees, hrly_rate_CAD):
    rng = range(0, num_employees)
    for n in rng:
        num_hrs_worked = int(input(('Input the number of worked hours: ')))
        pay = 0.0
        if num_hrs_worked < 40:
            pay = num_hrs_worked * hrly_rate_CAD
        if num_hrs_worked > 40:
            pay = num_hrs_worked * (hrly_rate_CAD * 1.5)
        print(f"the employee worked for {num_hrs_worked} and has earned {pay}")

Based on your comment, if you would like to input all of the worked hours first and then output the pay you should use a list. Here you will prompt and calculate all pay first, save it off to a list, then loop over the list displaying pay.

def calculate_total_pay_cad(num_employees, hrly_rate_CAD):
    rng = range(0, num_employees)
    pay_list = []
    for n in rng:
        num_hrs_worked = int(input(('Input the number of worked hours: ')))
        if num_hrs_worked < 40:
            pay_list.append((num_hrs_worked * hrly_rate_CAD, num_hrs_worked))
        if num_hrs_worked > 40:
            pay_list.append((num_hrs_worked * (hrly_rate_CAD * 1.5), num_hrs_worked))
    for pay, num_hrs_worked in pay_list:
        print(f"the employee worked for {num_hrs_worked} and has earned {pay}")
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to display as more of of all the inputs in the order like in the first display of outputs !e: insert the number of worked hours: 60 insert the number of worked hours:20 the employee worked for 60 hours and the earned is 700.00$ the employee worked for 20 hours and the earned is 200.00$
@jump In that case you should use a list. Please see my edit for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.