This is the code that I have written so far:-
# checking for the valididy of the marks entered
def read_marks(quiz, exam, assignment, project):
if quiz < 0:
print("ERROR: Invalid Marks", quiz, "< 0")
elif quiz > 20:
print("ERROR: Invalid Marks", quiz, "> 20")
elif exam < 0:
print("ERROR: Invalid Marks", exam, "< 0")
elif exam > 100:
print("ERROR: Invalid Marks", exam, "> 100")
elif assignment < 0:
print("ERROR: Invalid Marks", assignment, "< 0")
elif assignment > 100:
print("ERROR: Invalid Marks", assignment, "> 100")
elif project < 0:
print("ERROR: Invalid Marks", project, "< 0")
elif project > 50:
print("ERROR: Invalid Marks", project, "> 50")
else:
return quiz, exam, assignment, project
# computing GPA of the valid marks
def compute_gpa(quiz, exam, assignment, project):
a_wei = float(15)
b_wei = float(40)
c_wei = float(20)
d_wei = float(25)
GPA = ((quiz / 20 * a_wei) + (exam / 100 * b_wei) + (assignment / 100 * c_wei) + (project / 50 * d_wei)) / 10
GPA = round(GPA, 2)
return GPA
# assigning grade according to the GPA
def assign_grade(gpa):
if gpa == 10:
return "O"
elif gpa <= 4:
return "F"
elif gpa >= 9 and gpa < 10:
return "A"
elif gpa >= 8 and gpa < 9:
return "B"
elif gpa >= 6 and gpa < 8:
return "C"
elif gpa >= 5 and gpa < 6:
return "D"
elif gpa >= 4 and gpa < 5:
return "E"
def main():
# take input and call functions
a = int(input())
b = int(input())
c = int(input())
d = int(input())
x,l,m,r = read_marks(a, b, c, d)
q = compute_gpa(x,l,m,r)
v = q
s = assign_grade(v)
print("The GPA is " + str(q) + ", and the Grade is " + str(s))
if __name__ == "__main__":
main()
So my task was that I have to take 4 inputs and then check their validity, then compute their GPA and then print out their grade. This code is working good for numbers that are already in their limit, but if someone enters a number outside its limit it prints what I want it to print but it is also trying to compute the GPA of the error I am printing. Pls help as I have also tried raising an exception and it didn't work.