0
Python core 78.2. Error: Calculator has no attribute add. Pls help
class Calculator: def __init__(self,numbers): self.numbers =numbers def __add__(self,other): return self.numbers+other.numbers n1 = int(input()) n2 = int(input()) print(Calculator.add(n1,n2))
6 odpowiedzi
+ 1
Thank you all. I solved it.
This is how it is expected to be solved as the problem comes as a part of static method:
class Calculator:
def __init__(self,x,y):
self.x = x
self.y = y
@staticmethod
def __add__(x,y):
return x+y
n1= int(input())
n2 =int(input())
print(Calculator.__add__(n1,n2))
+ 1
You're overloading the + operator, it's not the declaration of the add function
+ 1
class Calculator:
def __init__(self,numbers):
self.numbers =numbers
def __add__(self,other):
return self.numbers+other.numbers
n1 = int(input())
n2 = int(input())
c1 = Calculator(n1)
c2 = Calculator(n2)
print(c1+c2)
+ 1
If you don't want to create the c1 and c2 variables then use:
class Calculator:
def __init__(self,numbers):
self.numbers =numbers
def __add__(self,other):
return self.numbers+other.numbers
n1 = int(input())
n2 = int(input())
print(Calculator(n1)+Calculator(n2))
+ 1
Thank you all.
I had solved just like how Eashan Morajkar solved. But still I had a doubt.
And I couldn't go to the next lesson.
class Calculator:
def __init__(self,numbers):
self.numbers =numbers
def __add__(self,other):
return self.numbers+other.numbers
n1= Calculator(int(input()))
n2 = Calculator(int(input()))
print(n1+n2)