0

## 為甚麼會出錯?不能理解(PYTHON)

``````class Calc:
def plus(self, a, b):
return a + b
def minus(self, a, b):
return a - b
a = Calc.plus(1, 2)
b = Calc.minus(2, 1)
print(a)
print(b)
``````

``````File "c:\Users\ericc\Downloads\python程式\Calc.py", line 6, in <module>
a = Calc.plus(1, 2)
TypeError: plus() missing 1 required positional argument: 'b'
``````

``````class Calc:
def plus(self, a, b):
self.a = a
self.b = b
return self.a + self.b
def minus(self, a, b):
self.a = a
self.b = b
return self.a - self.b
a = Calc.plus(1, 2)
b = Calc.minus(2, 1)
print(a)
print(b)
``````

Error變成了

``````File "c:\Users\ericc\Downloads\python程式\Calc.py", line 14
self.b = b
^
TabError: inconsistent use of tabs and spaces in indentation
``````

### 2 個回答

5
dragonH
iT邦超人 5 級 ‧ 2020-10-26 16:08:12

code

``````class Calc:
def __init__(self, a, b):
self.a = a
self.b = b
def plus(self):
return self.a + self.b
def minus(self):
return self.a - self.b

Calc(3, 2).plus() # return 5
Calc(3, 2).minus() # return 1
``````

ideone

dragonH iT邦超人 5 級 ‧ 2020-10-26 16:17:04 檢舉

``````a = Calc().plus(1, 2)
b = Calc().minus(2, 1)
print(a) // 3
print(b) // 1
``````
1
brayce
iT邦新手 5 級 ‧ 2020-10-27 18:07:58

``````class Calc:
@staticmethod
def plus(a, b):
return a + b

@staticmethod
def minus(a, b):
return a - b

a = Calc.plus(1, 2)
b = Calc.minus(2, 1)
print(a)
print(b)
``````
marlin12 iT邦研究生 5 級 ‧ 2020-12-23 23:56:23 檢舉