I keep getting an attribute error 'int' object has no attribute 'dollars'
<p>Im working on this Money class and everything worked fine up until the multiplication. I keep getting an attribute error and can't figure out where I'm going wrong. The multiplication is of type float.</p>

<pre><code>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 &gt; 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 &lt; 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 &gt; 99:
            d *= 1
            c *= 100
        return Money(d,c)


&gt;&gt;&gt; m2 = Money(10,10)
&gt;&gt;&gt; m2.times(3)
Traceback (most recent call last): File "&lt;pyshell#51&gt;", line 1, in &lt;module&gt; 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'
</code></pre>

<p>In <code>m2.times(3)</code>, you're passing the <code>int</code> <code>3</code> to the <code>times</code> method. In the times method, though, you're trying to multiply by <code>mult.dollars</code>, and not the <code>dollars</code> (<code>3</code>) that you actually passed. </p>

<p><code>mult.dollars</code> doesn't work like <code>self.dollars</code> would. In fact, it's not a valid construct at all. </p>

<p>Try</p>

<pre><code>&gt;&gt;&gt; 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 &gt; 99:
...             d *= 1
...             c *= 100
...         return Money(d, c)
</code></pre>

<p>You'll obviously have to modify the rest of your code, as well.</p>

<p>It seems you want to return a new <code>Money</code> object instead of a balance with each of these methods, but to demonstrate the point I made above:</p>

<pre><code>&gt;&gt;&gt; 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 &gt; 99:
...             d *= 1
...             c *= 100
...         return (d,c)
... 
&gt;&gt;&gt; m2 = Money(10, 10)
&gt;&gt;&gt; m2.times(3)
(30, 30)
</code></pre>

<p>Edit: Okay, the above doesn't seem to be what you're looking for, but I'll leave it for people running into a similar error. What you need to fix in your code is the <code>mult</code> object that you're trying to pass. Your <code>add</code> and <code>subtract</code> methods all have the same parameters: <code>self</code> and <code>other</code>, where <code>other</code> is another instance of the <code>Money</code> class, I presume. So, you're trying to multiply, add, or subtract different balances, basically? In that case, change the <code>mult.dollars</code> and <code>mult.cents</code> to <code>other.dollars</code> and <code>other.cents</code> so that you can access those attributes for another <code>Money</code> object.</p>

<p>After changing that:</p>

<pre><code>&gt;&gt;&gt; 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 &gt; 99:
...             d *= 1
...             c *= 100
...         return Money(d,c)
... 
&gt;&gt;&gt; m2 = Money(2, 3)
&gt;&gt;&gt; m3 = Money(4, 5)
&gt;&gt;&gt; m2.times(m3)
Money(8,15)
</code></pre>

<p>Also, you might want to look into the <code>d *= 1</code> and <code>c *= 100</code> lines, but that should answer your initial question.</p>

python class attributes