Calculate a student’s GPA
In this exercise, you write a Python program that calculates a student’s Grade Point Average (GPA). You practice storing values in variables, performing arithmetic operations, and converting user input from text to numbers so you can use it in calculations.
This exercise takes approximately 20 minutes.
Open the online Python IDE
-
Open a browser and navigate to the Python editor at https://buzahid.github.io/python-coder/app/.
- You’ll see two panels:
- Editor pane (left): where you write your Python code.
- Output terminal (right): where output is displayed and where you can type responses when the program prompts you.
- Clear any default code from the editor so you’re starting with a clean file.
Set up your starter code
Before writing any logic, you’ll paste a set of guiding comments into the editor. These comments act as an outline for your program — each one marks where a specific piece of code belongs.
-
Copy the following comments and paste them into the editor pane:
# Collect subject scores # Display the entered scores # Calculate the total, average, and GPA # Display the resultsRemember, comments are ignored by Python when the program runs. They’re just there to help you organize your code.
-
Leave the code as-is for now. In the steps that follow, you’ll add code beneath each comment.
Store grades as variables
You’ll start by storing a student’s subject scores as hardcoded variables. Each score is a number out of 100.
-
Beneath the
# Collect subject scorescomment, add the following lines:math_score = 88.0 english_score = 76.5 science_score = 91.0 history_score = 83.5Each variable holds a float — a decimal number. Even though some values like
88.0could be written as whole numbers, using floats from the start ensures your calculations return accurate decimal results later. -
Beneath the
# Display the entered scorescomment, add the following lines:print("Scores entered:") print(f" Math: {math_score}") print(f" English: {english_score}") print(f" Science: {science_score}") print(f" History: {history_score}") -
Select the ▶ (Run Code) button to run the program. You should see:
Scores entered: Math: 88.0 English: 76.5 Science: 91.0 History: 83.5
Calculate the total, average, and GPA
Now you’ll use arithmetic to add up the scores, calculate the average, and convert it to a GPA on a 4.0 scale using the formula:
\[GPA = \frac{average}{100} \times 4.0\]-
Beneath the
# Calculate the total, average, and GPAcomment, add the following lines of code:total = math_score + english_score + science_score + history_score num_subjects = 4 average = total / num_subjects gpa = (average / 100) * 4.0 -
Beneath the
# Display the resultscomment, add the following lines of code:print(f"\nTotal score: {total}") print(f"Number of subjects: {num_subjects}") print(f"Average score: {average}") print(f"\nStudent GPA: {round(gpa, 2)} / 4.0")Note:
\ninside an f-string adds a blank line before the text, which helps separate sections of output.round(gpa, 2)rounds the GPA to two decimal places. -
Run the program. Your complete output should now look like this:
Scores entered: Math: 88.0 English: 76.5 Science: 91.0 History: 83.5 Total score: 339.0 Number of subjects: 4 Average score: 84.75 Student GPA: 3.39 / 4.0
Accept grades from user input
Hardcoded values are useful for testing, but a real program should accept input from the user. The input() function always returns a string, so you need to convert it to a float before you can use it in calculations.
-
Replace the four hardcoded score variables beneath the
# Collect subject scorescomment with the followinginput()calls:print("Enter scores out of 100 for each subject:\n") math_score = float(input("Math score: ")) english_score = float(input("English score: ")) science_score = float(input("Science score: ")) history_score = float(input("History score: "))Wrapping
input()insidefloat()converts the text the user types into a decimal number in a single step. -
Keep the rest of your code exactly as it is. Your complete program should now look like this:
# Collect subject scores print("Enter scores out of 100 for each subject:\n") math_score = float(input("Math score: ")) english_score = float(input("English score: ")) science_score = float(input("Science score: ")) history_score = float(input("History score: ")) # Display the entered scores print("\nScores entered:") print(f" Math: {math_score}") print(f" English: {english_score}") print(f" Science: {science_score}") print(f" History: {history_score}") # Calculate the total, average, and GPA total = math_score + english_score + science_score + history_score num_subjects = 4 average = total / num_subjects gpa = (average / 100) * 4.0 # Display the results print(f"\nTotal score: {total}") print(f"Number of subjects: {num_subjects}") print(f"Average score: {average}") print(f"\nStudent GPA: {round(gpa, 2)} / 4.0") -
Run the program. When each prompt appears in the output terminal, click on it, type a score, and press Enter.
-
Try entering the same values as before to verify your output matches the following:
Enter scores out of 100 for each subject: Math score: 88 English score: 76.5 Science score: 91 History score: 83.5 Scores entered: Math: 88.0 English: 76.5 Science: 91.0 History: 83.5 Total score: 339.0 Number of subjects: 4 Average score: 84.75 Student GPA: 3.39 / 4.0
Code with AI
Using an AI assistant (like Copilot) is great way to explore a programming language at your own pace. Try each prompt below, read the response carefully, and test the code examples it gives you in the online editor.
Discovery 1: Find the sum of a group of numbers
AI Prompt:
“In Python, what’s a quick way to find the sum of a group of numbers? Can you show me beginner friendly examples?”
After the AI responds: Take a look at the examples the AI gives you. Do you see a way to simplify your total score calculation? Try it out in your program and see if it works as expected.