i am writing a class in python and in that i am calling a function to display variable argument.In the beginning the function calculates the sum of integers.
When i am calling the function without any class,then it is printing the values but inside the class,it is throwing error.
def displayString(name, msg = "Good morning!"):
print("Hello",name + ', ' + msg)
displayString("Kate")
displayString("Bruce","How do you do?")
the above is displaying the result but when i am calling the function inside the class below, it is throwing error
class new:
answer=0
def __init__(self,f,s):
self.first=f
self.second=s
def display(self):
print("this is the first ={} and the second={} value".format(self.first,self.second))
print("the answer is {}".format(self.answer))
def calculate(self):
self.answer=self.first+self.second
def displayString(name, msg = "Good morning!"):
print("Hello",name + ', ' + msg)
if __name__=="__main__":
x=new(1,2)
x.calculate()
x.display()
x.displayString("kate")
x.displayString("micky","How are you?")
TypeError Traceback (most recent call last)
<ipython-input-38-fd51df6426ac> in <module>
26 x.calculate()
27 x.display()
---> 28 x.displayString("kate")
29 x.displayString("micky","How are you?")
30
<ipython-input-38-fd51df6426ac> in displayString(name, msg)
18
19 def displayString(name, msg = "Good morning!"):
---> 20 print("Hello",name + ', ' + msg)
21
22
TypeError: unsupported operand type(s) for +: 'new' and 'str'
def displayString(self, name, msg = "Good morning!")