1

I am required to make a function that provides a Semester Summary that includes courses taken, credits taken, GPA points, and Semester GPA. I have this first function working

- gpacalc()

However, when I try making the second function

- coursePoints(credit,grade)

Which is supposed to return the "GPA points" of one specific class when you enter the credit of the class and the grade received. This is where I run into issues. It says "gp" isn't defined. I know this is a lot but I think it is probably a simple error. I am probably shadowing my variables, which I still can't quite figure out. If you can help, I appreciate it!

# coursePoints requirement
def coursePoints(credit, grade):
    gp = 0.00
    totalcredits = 0
    totalpoints = 0
    # I have lots of if statements here, I deleted them for simplicity.#
    gp = round(totalpoints,2)/round(totalcredits)

print("The GPA points of this class is:", round(gp))

coursePoints(3,"b")
8
  • 1
    Did you remember to declare "gp" as global variable? Commented Apr 4, 2020 at 0:39
  • 1
    Why not return gp from coursePoints and print the result of the function call? Commented Apr 4, 2020 at 0:49
  • 1
    The print() line at the end needs to be indented so it's part of the function. Commented Apr 4, 2020 at 0:53
  • 1
    BTW, the fact that you have a large block of identical code in both functions that converts letter grades to points suggests that you should create a separate function for that operation. Commented Apr 4, 2020 at 0:55
  • 1
    Good point. I will edit it to make it more simplified. Commented Apr 4, 2020 at 1:06

1 Answer 1

2

You have to add an indentation on print("The GPA points of this class is:", round(gp)) for it to be inside your definition of function coursePoints().

Without the indentation, it's as if you're trying to print a variable in your file that hasn't been given any value, nor declared.

Also, if you input "b" instead of "B" in coursePoints(), as per the last line, it will return a GPA of 0, since you've only specified capital letters. OR you add the option of analyzing bot letters in if with if grade in ["B","b"]:. OR an even simpler solution is to add grade = grade.upper() right below def coursePoints(credit, grade):, and mantain everything else the same (props to wjandrea).

The ending of the code should be:

[...]
    elif grade in ["D-","d-"]:
        totalpoints = totalpoints + (credit * .67)
        totalcredits = totalcredits + credit
    else:
        totalpoints = totalpoints + (credit * 0)
        totalcredits = totalcredits + credit
    gp = round(totalpoints,2)/round(totalcredits)
    # next line is indented now, thus, inside coursePoints()
    print("The GPA points of this class is:", round(gp))

coursePoints(3,"b") # change if condition for elif grade in ["B","b"]:

Edit: corrected and expanded as per wjandrea's comment.

Sign up to request clarification or add additional context in comments.

2 Comments

Correction: grade in ["B", "b"]. But it's easier to do grade.upper() == 'B' instead.
Thank you so much. Such an easy fix... and good catch on the capital letters! In my other function I have the .upper() to make them capital letters. In this function I will have to do the == ["b", "d-"].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.