Question
for line in data:#loop through each line in file values = line.strip().split()#split line into list of values student = values[0] total_credits = values[1] study_hours =
for line in data:#loop through each line in file values = line.strip().split("")#split line into list of values student = values[0] total_credits = values[1] study_hours = values[2] grade = values[3] total_students += 1 total_credits += total_credits total_study_hours += study_hours
average_credits = total_credits / total_students average_study_hours = total_study_hours / total_students THIS IS CODE I HAVE WRITTEN^^^^^^^^^^^^^^^^^^^^^^^ BELOW IS WHAT THE helper SAID WAS WRONG WITH MY CODE but i need help getting it fixed especially with the delimiter. I am not sure how to split line list using a delimiter to split my line list into values instead of characters.Can someone please help me figure out what he suggested about the delimiter and that i am not finding the total students, average study hours, or average credits--as he says i am overwriting them. Can someone help me with this part? I think i have fixed everything besides that small part. I will attach all of the code from that specific part below to make sure you have all the information you need.
This block is supposed to read the data from the HowManyHours.txt file and calculate the total students, total credits, total study hours, average credits, and average study hours for the students listed in the file. However, there are a few issues with this block. The strip().split("") line is splitting the line into a list of characters, not a list of values. You should use a delimiter (such as a comma) to split the line into a list of values. The variable total_students is not defined before it is used in this block. You should define it with a value of 0 before the loop. The variable credits is not defined, it should be total_credits The variable grade should be grade = values[3] This block of code is not being used to calculate the averages and totals, as the values are being overwritten. To sum up, you're mostly on the right track, but there are a few errors and improvements that can be made to the code to make it work correctly. Here is a modified version of your code that should work correctly:
def display_averages_and_totals(self): try: with open ("HowManyHours.txt", "r") as file:#read data from file data = file.readlines() except FileNotFoundError: raise FileNotFoundError("HowManyHours.txt is not found.")
total_credits = 0 total_study_hours = 0 total_students = 0
for student in self.students: total_credits += student.credits total_study_hours += student.study_hours average_credits = total_credits/ len(self.students) average_study_hours = total_study_hours/ len(self.students)
print("Study Hours Report ") print(f"Created for{self.report_for},{self.report_employee_id} ") print(f"Department:{self.report_department} ") print(f"Created By: {self.creator},{self.employee_id} ") print(f"Department:{self.department} ") print(f"Total Students: {len(self.students)} ") print(f"Average Credits: {average_credits:.2f} ") print(f"Average Study Hours: {average_study_hours:.2f} ") for line in data:#loop through each line in file values = line.strip().split("")#split line into list of values student = values[0] total_credits = values[1] study_hours = values[2] grade = values[3] total_students += 1 total_credits += total_credits total_study_hours += study_hours
average_credits = total_credits / total_students average_study_hours = total_study_hours / total_students print("Total Students:", total_students) print("Average Credits:", average_credits) print("Average Study Hours:", average_study_hours)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started