<p>你没有把<code>()</code>放在<code>theFraction</code>对象前面。即使你这样做了,你也将面临另一场灾难..你用两个变量(a,b)初始化你的对象,这意味着你将像</p>
<pre><code>Thefraction(a,b).add(a,b)
</code></pre>
<p>我不认为您需要这样做,因为<strong>a</strong>和<code>b</code>是每个方法中的局部变量。。这是一种你不需要的变量。
我想你想要的是这个。</p>
<pre><code> Thefraction(a,b).add()
</code></pre>
<p>这是完整的代码</p>
<pre><code>from fractions import Fraction
class Thefraction:
def __init__(self,a,b):
self.a = a
self.b =b
def add(self):
return self.a+ self.b
def subtract(self):
return self.a-self.b
def divide(self):
return self.a/self.b
def multiply(self):
return self.a/self.b
if __name__=='__main__':
try:
a = Fraction(input('Please type first fraction '))
b = Fraction(input('Please type second fraction '))
choice = int(input('Please select one of these 1. add 2. subtract 3. divide 4. multiply '))
if choice ==1:
print(Thefraction(a,b).add())
elif choice==2:
print(Thefraction(a,b).subtract())
elif choice==3:
print(Thefraction(a,b).divide())
elif choice==4:
print(Thefraction(a,b).multiply())
except ValueError:
print('Value error!!!!!')
</code></pre>