🎉 Welcome to PyVerse! Start Learning Today

Student Marks Average Calculator

Basic
Python

Calculate your average marks with Python

1) Project Overview

This project helps a student enter marks for their subjects and then calculates the average. The program:

  • Asks how many subjects there are.
  • Asks for each mark (for example, out of 100).
  • Calculates the total and the average.
  • Shows helpful information like highest and lowest mark.

It's simple, interactive, and perfect for practicing basic Python skills.

2) Learning Objectives

By completing this project, students will:

  • Use input() and print() to interact with the user.
  • Store data in variables and lists.
  • Use loops (for and while) to repeat actions.
  • Use if statements to check conditions (e.g., valid numbers).
  • Convert strings to numbers (int and float).
  • Do basic math (sum, average).
  • Format output neatly and write clear comments.

3) Step-by-Step Explanation

  1. Plan the inputs and outputs
    • Input: number of subjects (a positive whole number).
    • Input: each subject's mark (a number from 0 to 100).
    • Output: the list of marks, total, average (rounded), highest and lowest mark.
  2. Start the program with a friendly message
    • Print a welcome line so the user knows what the program does.
  3. Ask for the number of subjects
    • Use input().
    • Convert to int.
    • Keep asking until the number is a positive integer.
  4. Collect the marks in a list
    • Use a for loop to ask for each mark.
    • Convert to float (marks can have decimals like 92.5).
    • Check that each mark is between 0 and 100.
    • If the user types something invalid, ask again.
  5. Calculate total and average
    • Use sum(marks) for total.
    • Average = total / number of subjects.
  6. Show the results neatly
    • Print each mark with its subject number.
    • Print the total and average (rounded to 2 decimal places).
    • Print the highest and lowest mark.
  7. End with a friendly message
    • Encourage the user or suggest what to try next.

4) Complete and Well-Commented Python Code

You can copy and paste this into a file named marks_average_calculator.py and run it.

# Student Marks Average Calculator print("Welcome to the Student Marks Average Calculator!") print("This program will calculate the average of your subject marks.\n") # Step 1: Ask for the number of subjects (must be a positive whole number) while True: subjects_text = input("How many subjects do you have? ") try: num_subjects = int(subjects_text) if num_subjects <= 0: print("Please enter a number greater than 0.") else: break # valid number of subjects except ValueError: print("That's not a whole number. Please try again.") # Step 2: Collect marks for each subject in a list marks = [] # this list will store all the marks entered for i in range(1, num_subjects + 1): while True: mark_text = input(f"Enter mark for Subject {i} (0 to 100): ") try: mark = float(mark_text) # marks can be decimal numbers if 0 <= mark <= 100: marks.append(mark) break # valid mark, go to next subject else: print("Mark must be between 0 and 100. Please try again.") except ValueError: print("That doesn't look like a number. Please try again.") # Step 3: Calculate total and average total = sum(marks) average = total / num_subjects # Step 4: Show results neatly print("\n----- Results -----") print(f"Number of subjects: {num_subjects}") print("Marks you entered:") for i, m in enumerate(marks, start=1): # :.2f means show the number with 2 decimal places print(f" Subject {i}: {m:.2f}") print(f"\nTotal of marks: {total:.2f}") print(f"Average mark: {average:.2f}") # Extra helpful info: highest_mark = max(marks) lowest_mark = min(marks) print(f"Highest mark: {highest_mark:.2f}") print(f"Lowest mark: {lowest_mark:.2f}") # A friendly closing message if average >= 90: print("Excellent work! Keep it up!") elif average >= 70: print("Good job! You're doing well.") else: print("Keep practicing—you can improve with a bit more study.") print("\nThank you for using the Student Marks Average Calculator!")

5) Output Examples

Example 1: A smooth run

Welcome to the Student Marks Average Calculator!
This program will calculate the average of your subject marks.

How many subjects do you have? 4
Enter mark for Subject 1 (0 to 100): 85
Enter mark for Subject 2 (0 to 100): 92.5
Enter mark for Subject 3 (0 to 100): 78
Enter mark for Subject 4 (0 to 100): 66

----- Results -----
Number of subjects: 4
Marks you entered:
  Subject 1: 85.00
  Subject 2: 92.50
  Subject 3: 78.00
  Subject 4: 66.00

Total of marks: 321.50
Average mark: 80.38
Highest mark: 92.50
Lowest mark: 66.00
Good job! You're doing well.

Thank you for using the Student Marks Average Calculator!

Example 2: Handling invalid input

How many subjects do you have? -3
Please enter a number greater than 0.
How many subjects do you have? two
That's not a whole number. Please try again.
How many subjects do you have? 2
Enter mark for Subject 1 (0 to 100): 120
Mark must be between 0 and 100. Please try again.
Enter mark for Subject 1 (0 to 100): ninety
That doesn't look like a number. Please try again.
Enter mark for Subject 1 (0 to 100): 90
Enter mark for Subject 2 (0 to 100): 80

----- Results -----
Number of subjects: 2
Marks you entered:
  Subject 1: 90.00
  Subject 2: 80.00

Total of marks: 170.00
Average mark: 85.00
Highest mark: 90.00
Lowest mark: 80.00
Good job! You're doing well.

6) Small Exercise (Challenge)

Add a grade message based on the average

  • If average >= 90: print "Grade A"
  • If 80–89.99: print "Grade B"
  • If 70–79.99: print "Grade C"
  • If 60–69.99: print "Grade D"
  • If below 60: print "Grade E"

Tip: Use if/elif/else after calculating the average.

Extra idea: Also ask for subject names and print a tiny report like "Math: 85.00".

7) Summary

Great job! You built a working program that takes user input, validates it, stores it in a list, uses loops and conditions, and calculates an average. These are core programming skills you'll use in many projects. Keep practicing, try the challenge, and have fun improving your calculator!