2016-10-06 45 views
0

このMoneyクラスで作業していて、すべてが乗算されるまでうまくいきました。私は属性のエラーを取得し続けて、私が間違っているのを把握することはできません。乗算はfloat型です。 m2.times(3)属性エラー 'int'オブジェクトに属性 'dollars'がありません。

class Money: 
    def __init__(self, d, c): 
     self.dollars = d 
     self.cents = c 

    def __str__(self): 
     return '${}.{:02d}'.format(self.dollars, self.cents) 

    def __repr__(self): 
     return 'Money({},{})'.format(repr(self.dollars), self.cents) 

    def add(self, other): 
     d = self.dollars + other.dollars 
     c = self.cents + other.cents 
     while c > 99: 
      d += 1 
      c -= 100 
     return Money(d,c) 

    def subtract(self, other): 
     d = self.dollars - other.dollars 
     c = self.cents - other.cents 
     while c < 0: 
      d -= 1 
      c += 100 
     return Money(d,c) 

    def times(self, mult): 
     d = self.dollars * mult.dollars 
     c = self.cents * mult.cents 
     while c > 99: 
      d *= 1 
      c *= 100 
     return Money(d,c) 


>>> m2 = Money(10,10) 
>>> m2.times(3) 
Traceback (most recent call last): File "<pyshell#51>", line 1, in <module> m2.times(3) 
    File "/Users/kylerbolden/Desktop/hw2.py", line 67, in times 
    d = float(self.dollars) * float(mult.dollars) 
AttributeError: 'int' object has no attribute 'dollars' 
+0

コードブロックを変更してください。あなたのコードの最初の行は残りの部分から切り取られたようです! – Mangohero1

+1

してください、**してください**、あなたが得ている正確なエラーを投稿してください。または、あなたが提供した機能をどのように使っているかを少なくとも示してください。これがなければ、どの機能がエラーを起こしているのかを把握しようとする推測ゲームになります。 –

+0

>>>平方メートル=マネー(10,10) >>> m2.times(3) トレースバック(最新の呼び出しの最後): m2.timesで ファイル ""、ライン1、(3) ファイル "/Users/kylerbolden/Desktop/hw2.py"、行67、時には d = float(self.dollars)* float(mult.dollars) AttributeError: 'int'オブジェクトに属性 'dollars'がありません –

答えて

3

、あなたはtimes方法にint3を渡しています。しかし、timesメソッドでは、mult.dollarsで乗算しようとしていますが、実際に渡したdollars3)ではありません。

mult.dollarsは、self.dollarsのようには機能しません。実際、それはまったく有効な構成ではありません。

はあなたが明らかにも、あなたのコードの残りの部分を変更する必要があります

>>> class Money: 
...  def __init__(self, d, c): 
...   self.dollars = d 
...   self.cents = c 
...  def times(self, mult): 
...   d = self.dollars * mult 
...   c = self.cents * mult 
...   while c > 99: 
...    d *= 1 
...    c *= 100 
...   return Money(d, c) 

を試してみてください。

あなたの代わりにこれらのメソッドのそれぞれとのバランスの新しいMoneyオブジェクトを返すようにしたいようだが、私は上記の作っポイントを証明するために:

>>> class Money: 
...  def __init__(self, d, c): 
...   self.dollars = d 
...   self.cents = c 
...  def times(self, mult): 
...   d = self.dollars * mult 
...   c = self.cents * mult 
...   while c > 99: 
...    d *= 1 
...    c *= 100 
...   return (d,c) 
... 
>>> m2 = Money(10, 10) 
>>> m2.times(3) 
(30, 30) 

編集:さて、上記のようではありませんがあなたが探しているものになることができますが、私は同じようなエラーに遭遇している人々のためにそれを残します。あなたのコードで修正する必要があるものは、あなたが渡そうとしているmultオブジェクトです。 addsubtractのメソッドはすべて同じパラメータを持っています:selfotherここで、otherMoneyクラスの別のインスタンスです。だから、基本的に、さまざまな残高を増やしたり、追加したり、差したりしようとしているのですか?その場合、別のMoneyオブジェクトの属性にアクセスできるように、mult.dollarsmult.centsother.dollarsother.centsに変更してください。

その変更後:また

>>> class Money: 
...  def __init__(self, d, c): 
...   self.dollars = d 
...   self.cents = c 
...  def times(self, other): 
...   d = self.dollars * other.dollars 
...   c = self.cents * other.cents 
...   while c > 99: 
...    d *= 1 
...    c *= 100 
...   return Money(d,c) 
... 
>>> m2 = Money(2, 3) 
>>> m3 = Money(4, 5) 
>>> m2.times(m3) 
Money(8,15) 

、あなたはd *= 1c *= 100行に見たいと思うかもしれませんが、それはあなたの最初の質問に答える必要があります。

+0

他の関数に問題はありません。乗算関数のみです。私はあなたの方法を試してみると、ドルは定義されていないと言います。 –

+0

すべての綴りが正しいことを確認してください。 – blacksite

+1

他の機能はどのように機能していますか?例えば、あなたのadd関数では、引数の 'other'パラメータが' int'型として渡されています。私は仮定しています。関数定義で 'other.dollars'を呼び出しています。それは属性 "ドル"を持っていません。それは理にかなっていますか? – Mangohero1

関連する問題