-2

I'm trying to apply multilevel inheritance for student DB. I have name, rollno, age, marks, total and average. I want the outputs to be in the form of

Abc 1 21
Abc 1 21 98 79 88
Abc 1 21 98 79 88 255 85

The error I'm facing is that i cant concatenate str and int. I can understand that the problem is due to name in str and the rest in int. Can anyone help me out?

Here are my codes

import sys
class Person:
    def __init__(self, name,rollno,age):
        self.name = name
        self.rollno = rollno
        self.age=age
    def Name(self):
        return self.name + ", " + self.rollno + ",  "+ self.age

class Employee(Person):

    def __init__(self, name,rollno,age,m1,m2,m3):
        Person.__init__(self,name,rollno,age)
        self.m1 = m1
        self.m2 = m2
        self.m3 = m3

    def Getemployee(self):
        return self.name +"," + self.rollno +" ,"+ self.age +" ,"+ self.m1 +" ,"+ self.m2 +", "+ self.m3

class EmployeeDetails(Employee):
    def __init__(self, name,rollno,age,m1,m2,m3,total):
        Employee.__init__(self,name,rollno,age,m1,m2,m3)
        self.total=self.m1+self.m2+self.m3

    def empl(self):
        return self.name +" ,"+  self.rollno +" ,"+ self.age +", "+ self.m1 +", "+ self.m2 +" ,"+  self.m3 +" ,"+ self.total   
def main():
    try:
        n=int(raw_input("Enter the number of employees:"))
        for i in range(n):

            name =raw_input("enter the name:")
            rollno =raw_input("enter the rollno:")
            age =raw_input("enter the age:")
            m1 =raw_input("enter m1:")
            m2 =raw_input("enter m2:")
            m3 =raw_input("enter m3:")
            total =self.m1+self.m2+self.m3
            p = Person(name,rollno,age)
            e = Employee(name,rollno,age,m1,m2,m3)
            s= EmployeeDetails(name,rollno,age,m1,m2,m3,total)
            print p.Name()
            print e.Getemployee()
            print s.empl()

    except ValueError :
        print "value in error"
if __name__=='__main__':        
    main()
anonymous
  • 143
  • 1
  • 2
  • 16

1 Answers1

1

main() is not a method of any class; so self is not defined there. So, change total =self.m1+self.m2+self.m3 to total =m1+m2+m3. Also, every user input has been defined to be a string with raw_input(), except n. So, concatenation would work just fine.

Edit: To answer your comment, you just need to do type conversion. Try better! Also, change to this self.total=str(int(self.m1)+int(self.m2)+int(self.m3)) in __init__ method of EmployeeDetails class to get the required results.

P.S.: You might like to read this post on SO on the purpose of self.

Community
  • 1
  • 1
devautor
  • 2,506
  • 4
  • 21
  • 31
  • My program considers m1,m2,m3 as string and so for total i get output as m1m2m3 values and not the sum of it. If i change total=int(m1)+int(m2)+int(m3), then i dont get the desired output in s.empl as total is in int and name will be in string, i get the error that it cant concatenate str and int – anonymous Apr 07 '17 at 12:06
  • no it didnt. getting output of total as concatenation of strings instead of adding up its values. eg: m1=2, m2=3, m3=4. my total should be 9 but im getting it as 234 – anonymous Apr 07 '17 at 12:27
  • 1
    I main(), have total = str(int(m1)+int(m2)+int(m3)) and in EmployeeDetails class, have self.total = total. This works with me! – devautor Apr 07 '17 at 12:43
  • Ur solution works. Thanks :) My question needs to be downvoted? – anonymous Apr 07 '17 at 12:54
  • 1
    Those who have down-voted must have felt that you are capable of googling how to cast datatypes. But, there's no worry. Everybody starts at the start. We learn :) – devautor Apr 07 '17 at 12:57