Nested loop to take input 5 times and display total and average
Question:
hoursWorked = 0
researchAssistants = 3
for assisstant in range(researchAssistants):
for day in range(5):
if day == 0:
hoursWorked += float(input("Enter hours for research assistant {0} for Day 1: ".format(assisstant+1)))
if day == 1:
hoursWorked += float(input("Enter hours for research assistant {0} for Day 2: ".format(assisstant+1)))
if day == 2:
hoursWorked += float(input("Enter hours for research assistant {0} for Day 3: ".format(assisstant+1)))
if day == 3:
hoursWorked += float(input("Enter hours for research assistant {0} for Day 4: ".format(assisstant+1)))
if day == 4:
hoursWorked += float(input("Enter hours for research assistant {0} for Day 5: ".format(assisstant+1)))
print()
print("Research assistant {0} worked in total".format(assisstant+1), hoursWorked, "hours")
avgHoursWorked = hoursWorked / 5
if avgHoursWorked > 6:
print("Research assistant {0} has an average number of hours per day above 6".format(assisstant+1) )
print()
I want the code to take amount of hours worked per day in a 5-day work week for three employees. I then want it to summarize it to hours/week for each employee. If an employee’s average number of hours per day is above 6, the program should flag this.
So far I the program takes the input, and gives the total. But the average is wrong. I believe avgHoursWorked
should be in the nested for loop – but this does not really work for me. I would rather want the input to be taken first, then display the totals and flag an avg. >6 at the end.
Edit:
Here is the output as per above code.
Enter hours for research assistant 1 for Day 1: 5
Enter hours for research assistant 1 for Day 2: 5
Enter hours for research assistant 1 for Day 3: 5
Enter hours for research assistant 1 for Day 4: 5
Enter hours for research assistant 1 for Day 5: 5
Research assistant 1 worked in total 25.0 hours
Enter hours for research assistant 2 for Day 1: 5
Enter hours for research assistant 2 for Day 2: 5
Enter hours for research assistant 2 for Day 3: 5
Enter hours for research assistant 2 for Day 4: 5
Enter hours for research assistant 2 for Day 5: 5
Research assistant 2 worked in total 50.0 hours
Research assistant 2 has an average number of hours per day above 6
Enter hours for research assistant 3 for Day 1: 5
Enter hours for research assistant 3 for Day 2: 5
Enter hours for research assistant 3 for Day 3: 5
Enter hours for research assistant 3 for Day 4: 5
Enter hours for research assistant 3 for Day 5: 5
Research assistant 3 worked in total 75.0 hours
Research assistant 3 has an average number of hours per day above 6
In this case, the hours worked are being added-up where they should really be per individual Research Assistant
Answers:
It sounds like you meant to reset hoursWorked
for each assistant:
researchAssistants = 3
for assisstant in range(researchAssistants):
hoursWorked = 0
for day in range(5):
...
hoursWorked = 0
researchAssistants = 3
for assisstant in range(researchAssistants):
for day in range(5):
if day == 0:
hoursWorked += float(input("Enter hours for research assistant {0} for Day 1: ".format(assisstant+1)))
if day == 1:
hoursWorked += float(input("Enter hours for research assistant {0} for Day 2: ".format(assisstant+1)))
if day == 2:
hoursWorked += float(input("Enter hours for research assistant {0} for Day 3: ".format(assisstant+1)))
if day == 3:
hoursWorked += float(input("Enter hours for research assistant {0} for Day 4: ".format(assisstant+1)))
if day == 4:
hoursWorked += float(input("Enter hours for research assistant {0} for Day 5: ".format(assisstant+1)))
print()
print("Research assistant {0} worked in total".format(assisstant+1), hoursWorked, "hours")
avgHoursWorked = hoursWorked / 5
if avgHoursWorked > 6:
print("Research assistant {0} has an average number of hours per day above 6".format(assisstant+1) )
print()
I want the code to take amount of hours worked per day in a 5-day work week for three employees. I then want it to summarize it to hours/week for each employee. If an employee’s average number of hours per day is above 6, the program should flag this.
So far I the program takes the input, and gives the total. But the average is wrong. I believe avgHoursWorked
should be in the nested for loop – but this does not really work for me. I would rather want the input to be taken first, then display the totals and flag an avg. >6 at the end.
Edit:
Here is the output as per above code.
Enter hours for research assistant 1 for Day 1: 5
Enter hours for research assistant 1 for Day 2: 5
Enter hours for research assistant 1 for Day 3: 5
Enter hours for research assistant 1 for Day 4: 5
Enter hours for research assistant 1 for Day 5: 5
Research assistant 1 worked in total 25.0 hours
Enter hours for research assistant 2 for Day 1: 5
Enter hours for research assistant 2 for Day 2: 5
Enter hours for research assistant 2 for Day 3: 5
Enter hours for research assistant 2 for Day 4: 5
Enter hours for research assistant 2 for Day 5: 5
Research assistant 2 worked in total 50.0 hours
Research assistant 2 has an average number of hours per day above 6
Enter hours for research assistant 3 for Day 1: 5
Enter hours for research assistant 3 for Day 2: 5
Enter hours for research assistant 3 for Day 3: 5
Enter hours for research assistant 3 for Day 4: 5
Enter hours for research assistant 3 for Day 5: 5
Research assistant 3 worked in total 75.0 hours
Research assistant 3 has an average number of hours per day above 6
In this case, the hours worked are being added-up where they should really be per individual Research Assistant
It sounds like you meant to reset hoursWorked
for each assistant:
researchAssistants = 3
for assisstant in range(researchAssistants):
hoursWorked = 0
for day in range(5):
...