Computer Science, asked by sindhuvijayakumarkol, 5 months ago

Create a class student having data members name, age, roll no, marks of 3 subjects, and total marks. Write a python program to read the details of n student ,find the total mark and display it.

Answers

Answered by allysia
88

Answer:

Creating table though python:

import mysql.connect

mydb=mysql.connector.connect(host="localhost", user="milktea", passwd="4",  database="brainly")

mycursor=mydb.cursor()

mycursor.execute("Create table class( name varchar (20), age int, roll_no int, eng_marks int,maths_marks int, chemistry_marks int, total_marks int);")

mydb.commit()

print("table affected ")

For the second part i.e having data displayed use:

import mysql.connector

mydb=mysql.connector.connect(host="localhost", user="milktea", passwd="4",  database="brainly")

mycursor=mydb.cursor()

mycursor.execute("Select class.*, (eng_marks +maths_marks +chemistry_marks) as total_marks from student")

records=mycursor.fetchall()

n=int(input("Enter first n students to display: "))

if n>len(records):

   print("Only", len(records),"avaliable.")

   print()

else:

   for i in range(0, n):

       print(records[i], ", Total marks: ", records[i][-1] )

NOTE: You can directly use total_marks column since we have defined one in the table but since the question reads  "find the total mark and display it" I have used SUM and then displayed them.

Answered by anamikaaiju2008
33

Answer:

Explanation:

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 first subject: "))

m2 = int (input("Enter the marks in second subject: "))

m3 = int (input("Enter the marks in third subject: "))

s1 = Student()

s1.getData(r, name, m1, m2, m3)

s1.displayData()

Output:

Enter the roll number: 10

Enter the name: Mahesh Huddar

Enter the marks in first subject: 20

Enter the marks in second subject: 30

Enter the marks in 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

Similar questions