Python program that stores the student name, roll number, and marks in three subjects

Python program that stores the student name, roll number, and marks in three subjects

Problem Definition:

Write a Python program that has a class named Student that stores the student name, roll number, and marks in three subjects. Display the student information (Roll No, Name, and Total Marks scored)

Video Tutorial

Source code for Python program that stores the name, roll number, and marks in three subjects

class Student:
    marks = []
    def getData(self, rn, name, m1, m2, m3):
        Student.rn = rn
        Student.name = name
        Student.marks.append(m1)
        Student.marks.append(m2)
        Student.marks.append(m3)
        
    def displayData(self):
        print ("Roll Number is: ", Student.rn)
        print ("Name is: ", Student.name)
        #print ("Marks in subject 1: ", Student.marks[0])
        #print ("Marks in subject 2: ", Student.marks[1])
        #print ("Marks in subject 3: ", Student.marks[2])
        print ("Marks are: ", Student.marks)
        print ("Total Marks are: ", self.total())
        print ("Average Marks are: ", self.average())
        
    def total(self):
        return (Student.marks[0] + Student.marks[1] +Student.marks[2])
    
    def average(self):
        return ((Student.marks[0] + Student.marks[1] +Student.marks[2])/3)
    
r = int (input("Enter the roll number: "))
name = input("Enter the name: ")
m1 = int (input("Enter the marks in the first subject: "))
m2 = int (input("Enter the marks in the second subject: "))
m3 = int (input("Enter the marks in the third subject: "))

s1 = Student()
s1.getData(r, name, m1, m2, m3)
s1.displayData()

Output:

Enter the roll number: 10

See also  Python program to find best of two test average marks

Enter the name: Mahesh Huddar

Enter the marks in the first subject: 20

Enter the marks in the second subject: 30

Enter the marks in the third subject: 25

Roll Number is: 10

Name is: Mahesh Huddar

Marks are: [20, 30, 25]

Total Marks are: 75

Average Marks are: 25.0

Summary:

This article discusses, how to write a Python program that stores the name, roll number, and marks in three subjects. Subscribe to our YouTube channel for more videos and like the Facebook page for regular updates.

Leave a Comment

Your email address will not be published. Required fields are marked *