Learning Objectives 1. Understand and be able to use string variables, strings a
ID: 3594998 • Letter: L
Question
Learning Objectives 1. Understand and be able to use string variables, strings as sequences and strings as operations 2. Understand character encoding 3. Understand string methods and string formatting 4. Familiarity with file processing in python Activities 1. Write a batch-oriented python program that does the following: 1. Explains to the user what it will do 2. Prompts the user for an input.dat file 3. Reads the names and scores from the input file 4. Prints to the screen the student names, students scores and student grades (based on 0-59 F, 60-69 D, 70-79 C, 80-89 B, 90-100 A) in the format:" First Name> Last Name> received a score of for a grade ofExplanation / Answer
Hi,
Find the Python code for the above scenario, as follows:
#Python code
#Accept the file from the user
filename=input("Enter a .dat file as input: ")
#Opens the file in READ mode with "r" option
fo=open(filename,"r")
#Create a new file with name grades.dat and open it in WRITE Mode to write the data after parsing the input file
fw=open("grades.dat","w")
grade=None
for line in fo:
line=line.split()#Every line in file is converted into a list where list[0],list[1],list[2] reflects Firstname,Lastname and Score
score=int(line[2])
#Decide Grade for the score obtained using the if-elif-else clause below
if (score>=90 and score<=100):
grade="A"
elif (score>=80 and score<=89):
grade="B"
elif (score>=70 and score<=79):
grade="C"
elif (score>=60 and score<=69):
grade="D"
else:
grade="F"
print (line[0]+" "+line[1]+" received a score of "+line[2]+" for a grade of "+grade)
#Write to grades.dat
fw.write(line[0]+" "+line[1]+" "+line[2]+" "+grade)
fw.write(" ")
#close the files
fw.close()
fo.close()
Thanks,
Hema
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.