Answered step by step
Verified Expert Solution
Question
1 Approved Answer
3 . Scores with Files Objectives Gain familiarity with CSV files Perform calculations with data from CSV files Read from multiple files in the same
Scores with Files
Objectives
Gain familiarity with CSV files
Perform calculations with data from CSV files
Read from multiple files in the same program
Description
Prompt the user for a text file that contains a list of student names, one on each line. Also prompt the user for a CSV file that contains scores for the students in the following order: homework
homework
midterm exam, final exam. Each line in the CSV has four scores, delimited by a comma. Each name in the text file has the corresponding scores in the CSV file.
The prompts should be:
Enter the text file:
Enter the CSV file:
Each prompt should have a space after the colon.
You will need to calculate the weighted final grade for each student as follows:
homework
homework
midterm exam
final exam
Homework is scored out of
points and exams are scored out of
points
You need to create a program that will read the scores from the CSV file, calculate the weighted final grade, and display the resulting final grade, along with the student
s name, to the standard display. You will not be writing to an output file.
You can assume that the same number of lines will be in the input files, and that the order of the names and corresponding grades is the same in the two files.
Two sample files have been created for you to practice with. names.txt and scores.csv
Display the name and final grade for all students in the following format:
Bill Gates earned
Mark Zuckerberg earned
Manually calculate the final grade so you can verify that your program is working correctly.
Run your program using TRY IT
to be sure it
s working. Open the input files and verify your program is printing the correct grades to the standard display. You can modify the data in the input files and rerun your program to be sure it is working correctly. Once your program is working correctly, click the Check It
buttons
Below I created this code:
import csv
# Function to calculate the weighted final grade
def calculate
final
grade
scores
:
homework
homework
test
final
test
scores
weighted
grade
homework
homework
test
final
test
return weighted
grade
# Prompt the user for input file names
names
file
input
Enter the text file:
scores
file
input
Enter the CSV file:
# Open the files for reading
with open
names
file,
r
as names
file, open
scores
file,
r
as scores
file:
# Read the names into a list
student
names
names
file.read
splitlines
# Create a CSV reader object for the scores file
csv
reader
csv
reader
scores
file
# Process each line of the CSV file and calculate the final grade
for name, row in zip
student
names, csv
reader
:
scores
int
x
for x in row
final
grade
calculate
final
grade
scores
# Format the output to one decimal place
print
f
name
earned
final
grade:
f
But when i use this code I get this message back:
Output:
Enter the text file: Enter the CSV file: Bill Gates earned
Mark Zuckerberg earned
Jeff Bezos earned
Jack Dorsey earned
Steve Wozniak earned
Expected:
Enter the text file: Enter the CSV file: Bill Gates earned
Mark Zuckerberg earned
Jeff Bezos earned
Jack Dorsey earned
Steve Wozniak earned
What should my code look like to get the proper output?
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