You have store a record of students in a file record.txt. To make a file: Make a function to write in a file write_data().Each record contains the student's name, and there marks in Maths, Physics and Chemistry out of 100(write a check to ensure the entered marks is in range 0-100, if user enters marks out of this range, ask the user to re-enter the marks ). The marks can be floating values. The user enters names and marks for students. You are required to save the record in a record.txt. Once you done with the making file. Make a function read_data(d) that takes empty dictionary as a parameter Read the data from record.txt and add it into dictionary as a name and percentage The user then enters a student's name. Output the name, average percentage marks obtained by that student, correct to two decimal places and on the basis of marks display grades as well
Weighted final score Final grade 80 <= mark <= 100 A 70 <= mark < 80 B 60 <= mark < 70 C 50 <= mark < 60 D mark < 50 E
def check_marks(marks):
if marks > 100 or marks < 0:
print("Re-Enter Maths Marks")
return False
return True
def write_data():
with open("record.txt", "a") as f:
name = input("Enter Student Name: ")
while True:
phy = float(input("Enter Physics Marks: "))
chem = float(input("Enter Chemistry Marks: "))
math = float(input("Enter Maths Marks: "))
if check_marks(math) and check_marks(phy) and check_marks(math):
st = name + " " + str(phy) + " " + str(chem) + " "+ str(math)
f.write(st + "\n")
break
def read_data(d):
with open("record.txt", "r") as f:
for line in f.readlines():
name, phy, chem, math = line.split(" ")
d[name] = (float(phy) + float(chem) + float(math)) / 3
return d
d = {}
write_data()
read_data(d)
name = input("Input name: ")
avg = d[name]
if avg < 50:
grade = "E"
elif avg < 60:
grade = "D"
elif avg < 70:
grade = "C"
elif avg < 80:
grade = "B"
else:
grade = "A"
print("{} - Average: {:.2f} - Grade: {}".format(name,avg,grade))
Comments
Leave a comment