We appreciate your visit to Problem 1 In this problem you will input an unknown quantity of grades from a file and calculate the average a Open the file grades. This page offers clear insights and highlights the essential aspects of the topic. Our goal is to provide a helpful and engaging learning experience. Explore the content and find the answers you need!
Answer :
Each problem involving reading data from a file, processing it, and outputting results. The code for each of these problems will be in Python. Make sure you have the input files ("grades.txt", "mixed.txt", and "mbed.txt") in the same directory as the script.
Problem 1 - Calculating Average Grades:
# Problem 1
total = 0
count = 0
with open("grades.txt", "r") as file:
for line in file:
grade = float(line.strip())
print(grade)
total += grade
count += 1
average = total / count
print(f"Class Average is {average:.2f} for {count} students")
Problem 2 - Calculating Average Grades with Student Names:
# Problem 2
total = 0
count = 0
with open("mixed.txt", "r") as file:
for line in file:
name, grade = line.strip().split()
grade = float(grade)
print(f"{name} {grade}")
total += grade
count += 1
average = total / count
print(f"Class Average is {average:.2f} for {count} students")
Problem 3 - Separating Passing and Failing Students:
# Problem 3
passing_students = []
failing_students = []
with open("mbed.txt", "r") as input_file, open("output.txt", "w") as output_file:
output_file.write("These are the students who passed the class:\n")
for line in input_file:
name, grade = line.strip().split()
grade = float(grade)
if grade >= 70:
passing_students.append((name, grade))
output_file.write(f"{name} {grade}\n")
output_file.write("\nThese are the students who failed the class:\n")
input_file.seek(0) # Rewind the file
for line in input_file:
name, grade = line.strip().split()
grade = float(grade)
if grade < 70:
failing_students.append((name, grade))
output_file.write(f"{name} {grade}\n")
print("The following students passed:")
for name, grade in passing_students:
print(f"{name} {grade}")
print("\nThe following students failed:")
for name, grade in failing_students:
print(f"{name} {grade}")
Make sure to replace the example file names ("grades.txt", "mixed.txt", and "mbed.txt") with the actual file names you have. The code provided reads the input files, processes the data as per the problem statements, and outputs the results as described.
Learn more about Python script click;
https://brainly.com/question/14378173
#SPJ4
Thanks for taking the time to read Problem 1 In this problem you will input an unknown quantity of grades from a file and calculate the average a Open the file grades. We hope the insights shared have been valuable and enhanced your understanding of the topic. Don�t hesitate to browse our website for more informative and engaging content!
- Why do Businesses Exist Why does Starbucks Exist What Service does Starbucks Provide Really what is their product.
- The pattern of numbers below is an arithmetic sequence tex 14 24 34 44 54 ldots tex Which statement describes the recursive function used to..
- Morgan felt the need to streamline Edison Electric What changes did Morgan make.
Rewritten by : Barada