Write A Python Class Named Student With Two Attributes Student_Name Marks

In this article let's learn how to write a python program to create a student class with two class variables named student_name and marks. Once we create a student class, let's define a function to compute the grade of a student based on marks.

This is a simple python program to demo the python class using student details, keep reading…

 

Write A Python Class Named Student With Two Attributes Student_Name Marks

 

 


Write A Python Class Named Student With Two Attributes Student_Name Marks

Let's take a class keyword and define the student class. In the default constructor, lets the user enter the student name and marks using input() function call. Once the user enters the student name and marks assign it to class variables.

Node defines another function compute_grade() and Use if statement to check if marks is less than 25 then print C Grade else if Marks are less than 60 print B grade, else print A grade.


class student:

   def __init__(self):
       self.student_name = input("Enter Student Name : ")
       self.marks = float(input("Enter Student Marks : "))

   def compute_grade(self):
       if self.marks < 25:
           print(f"{self.student_name} Scored C Grade")
       elif self.marks < 60:
           print(f"{self.student_name} Scored B Grade")
       else:
           print(f"{self.student_name} Scored A Grade")





In the main program take a student object and get the student class object by calling default constructor. Once we get the student object by initializing the student name and marks, then call the compute_grade() function to print the student grade.

student_obj = student()
student_obj.compute_grade()




Python Program
=======================

class student:

   def __init__(self):
       self.student_name = input("Enter Student Name : ")
       self.marks = float(input("Enter Student Marks : "))

   def compute_grade(self):
       if self.marks < 25:
           print(f"{self.student_name} Scored C Grade")
       elif self.marks < 60:
           print(f"{self.student_name} Scored B Grade")
       else:
           print(f"{self.student_name} Scored A Grade")


student_obj = student()
student_obj.compute_grade()





Output
================
Enter Student Name : Ana
Enter Student Marks : 35
Ana Scored B Grade





Output
===============
Enter Student Name : James
Enter Student Marks : 90
James Scored A Grade




Output
===============
Enter Student Name : Sara
Enter Student Marks : 14
Sara Scored C Grade




Conclusion
==================
Execute the above python program by providing the student name and marks scored and compute the grade.

Comment it down below if you have any suggestion to improve the above python program.




Post a Comment

0 Comments