string - Can't concatenate str and int in Python -
i'm trying apply multilevel inheritance student db. have name
, rollno
, age
, marks
, total
, average
. want outputs in form of
abc 1 21 abc 1 21 98 79 88 abc 1 21 98 79 88 255 85
the error i'm facing cant concatenate str , int. can understand problem due name in str , rest in int. can me out?
here 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 number of employees:")) in range(n): name =raw_input("enter name:") rollno =raw_input("enter rollno:") age =raw_input("enter 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()
main() not method of class; self
not defined there. so, change total =self.m1+self.m2+self.m3
total =m1+m2+m3
. also, every user input has been defined string raw_input(), except n. so, concatenation work fine.
edit: answer comment, need type conversion. try better! also, change self.total=str(int(self.m1)+int(self.m2)+int(self.m3))
in __init__
method of employeedetails
class required results.
p.s.: might read this post on so on purpose of self
.
Comments
Post a Comment