def _init_ (self):
__init__应该是两边各有两个_下划线
你写的_init_两边各只有一个_下划线
你题目的解答代码如下:
class Calculator():
def __init__ (self): #__init__应该是两边各有两个_下划线
self.operation_map = {'add':self.add,
'subtract':self.subtract,
'multiplication':self.multiplication,
'division':self.division}
def get_input(self):
self.num1 = int(input()) #加self.
self.num2 = int(input())
def execute_operation(self,peration):
return self.operation_map[peration]()
def add(self):
result = self.num1+self.num2
return result
def subtract(self):
result = self.num1-self.num2
return result
def multiplication(self):
result = self.num1*self.num2
return result
def division(self):
result = self.num1//self.num2
return result
p = Calculator()
p.get_input()
print(p.execute_operation("add"))
print(p.execute_operation("subtract"))
print(p.execute_operation("multiplication"))
print(p.execute_operation("division"))
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!