Home > Mobile >  Python: Program is only multiplying the last number the user inputs rather than all of them
Python: Program is only multiplying the last number the user inputs rather than all of them

Time:01-09

This is my first time posting. Please bare with me. I have been working on this program for about a month and I cannot get it to run properly. When the user runs the program, you will be asked to input the hours worked by four employees. The program will then multiply the hours worked for each employee and print their gross pay for each of them. The problem is, it is only multiplying the last number I input and prints that as the gross pay for all four employees. I could really use some help.

If you need more information or want me to clarify anything, please let me know. All help is appreciated. Thank you!

Start:

NUM_EMPLOYEES = 4 

employeehours = [ ] 

for i in range (NUM_EMPLOYEES):

    print('Enter the hours worked by employee ', i   1, ':', sep = '', end = ' ')
    employeehours = float(input())

pay_rate = 14

for i in range (NUM_EMPLOYEES):
   
    gross_pay = employeehours * pay_rate 
    print('Gross pay for employee', i   1, ': $', format(gross_pay , '.2f'), sep = ' ')

CodePudding user response:

you are setting employeehours to last floating value. your code is right almost.

NUM_EMPLOYEES = 4 

employeehours = [ ] 

for i in range (NUM_EMPLOYEES):

    print('Enter the hours worked by employee ', i   1, ':', sep = '', end = ' ')
    employeehours.append(float(input()))

pay_rate = 14

for i in range (NUM_EMPLOYEES):
   
    gross_pay = employeehours[i] * pay_rate 
    print('Gross pay for employee', i   1, ': $', format(gross_pay , '.2f'), sep = ' ')

CodePudding user response:

So it should look like this:

NUM_EMPLOYEES = 4
PAY_RATE = 14
employee_payment = []

for i in range(NUM_EMPLOYEES):
    hours = float(input(f'Enter the hours worked by employee {i 1}: '))
    employee_payment.append(hours * PAY_RATE)

for i in range(NUM_EMPLOYEES):
    print(f'Gross pay for employee {i}: {employee_payment[i]}')

At the beginning you defined a list, and then you are assigning the value to the same variable, thats why the output is only one employee payment

  •  Tags:  
  • Related