Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Nested loop to take input 5 times and display total and average

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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):
        ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading