Answered step by step
Verified Expert Solution
Link Copied!

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

3
.
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
1
,
homework
2
,
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:
5
%
homework
1
5
%
homework
2
4
0
%
midterm exam
5
0
%
final exam
Homework is scored out of
1
0
points and exams are scored out of
1
0
0
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
8
1
.
5
%
Mark Zuckerberg earned
9
9
.
6
%
.
.
.
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
1
,
homework
2
,
test
1
,
final
_
test
=
scores
weighted
_
grade
=
0
.
0
5
*
homework
1
+
0
.
0
5
*
homework
2
+
0
.
4
*
test
1
+
0
.
5
*
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:
.
1
f
}
%
"
)
But when i use this code I get this message back:
Output:
Enter the text file: Enter the CSV file: Bill Gates earned
7
3
.
8
%
Mark Zuckerberg earned
9
0
.
6
%
Jeff Bezos earned
5
2
.
8
%
Jack Dorsey earned
8
1
.
9
%
Steve Wozniak earned
6
8
.
6
%
Expected:
Enter the text file: Enter the CSV file: Bill Gates earned
8
1
.
5
%
Mark Zuckerberg earned
9
9
.
6
%
Jeff Bezos earned
5
5
.
5
%
Jack Dorsey earned
9
0
.
0
%
Steve Wozniak earned
7
4
.
9
%
What should my code look like to get the proper output?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Wait Statistics

Authors: Enrico Van De Laar

1st Edition

1484211391, 9781484211397

More Books

Students also viewed these Databases questions

Question

Determine miller indices of plane X z 2/3 90% a/3

Answered: 1 week ago

Question

List the advantages and disadvantages of the pay programs. page 536

Answered: 1 week ago