-1

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.

  • 1
    You could raise an exception. – khelwood Jul 28 '21 at 08:16
  • How do I do that? – Soham Sharma Jul 28 '21 at 08:17
  • 1
    You can raise an exception, e.g. `raise ValueError("assignment too high")`. Or you could exit the app if you don't care about recovery, e.g. `sys.exit(1)` after printing the error message. It depends on what you want to do after detecting an error. – Tom Karzes Jul 28 '21 at 08:19
  • You could also do some input validations in your ```main()``` function and only call the functions if the inputs are within limits. – Ram Jul 28 '21 at 08:21
  • so raising an exception it is not going through the whole read_marks function but I have taken care of it already, the problem is that after it is going through read_marks function it is getting returned a string, that it is trying to calculate the GPA of. I want to stop the code after it has gone through the function and if it has gotten a string as the result. – Soham Sharma Jul 28 '21 at 08:26

1 Answers1

0

Instead of printing the error you could just raise the exception in read_marks function like this :

# checking for the valididy of the marks entered 
def read_marks(quiz, exam, assignment, project):
    if quiz < 0:
        raise Exception("ERROR: Invalid Marks", quiz, "< 0")
    elif quiz > 20:
        raise Exception("ERROR: Invalid Marks", quiz, "> 20")
    elif exam < 0:
        raise Exception("ERROR: Invalid Marks", exam, "< 0")
    elif exam > 100:
        raise Exception("ERROR: Invalid Marks", exam, "> 100")
    elif assignment < 0:
        raise Exception("ERROR: Invalid Marks", assignment, "< 0")
    elif assignment > 100:
        raise Exception("ERROR: Invalid Marks", assignment, "> 100")
    elif project < 0:
        raise Exception("ERROR: Invalid Marks", project, "< 0")
    elif project > 50:
        raise Exception("ERROR: Invalid Marks", project, "> 50")
    else:
        return quiz, exam, assignment, project
Lakshika Parihar
  • 1,017
  • 9
  • 19