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))

30th Sep 2021, 2:54 PM
Suramuthu R
Suramuthu R - avatar
6 Antworten
+ 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))
1st Oct 2021, 2:33 AM
Suramuthu R
Suramuthu R - avatar
+ 1
You're overloading the + operator, it's not the declaration of the add function
30th Sep 2021, 3:03 PM
Eashan Morajkar
Eashan Morajkar - avatar
+ 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)
30th Sep 2021, 3:07 PM
Eashan Morajkar
Eashan Morajkar - avatar
+ 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))
30th Sep 2021, 3:36 PM
Eashan Morajkar
Eashan Morajkar - avatar
+ 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)
30th Sep 2021, 3:43 PM
Suramuthu R
Suramuthu R - avatar