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.
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'
In m2.times(3)
, you're passing the int
3
to the times
method. In the times method, though, you're trying to multiply by mult.dollars
, and not the dollars
( 3
) that you actually passed.
mult.dollars
doesn't work like self.dollars
would. In fact, it's not a valid construct at all.
Try
>>> 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)
You'll obviously have to modify the rest of your code, as well.
It seems you want to return a new Money
object instead of a balance with each of these methods, but to demonstrate the point I made above:
>>> 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)
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 mult
object that you're trying to pass. Your add
and subtract
methods all have the same parameters: self
and other
, where other
is another instance of the Money
class, I presume. So, you're trying to multiply, add, or subtract different balances, basically? In that case, change the mult.dollars
and mult.cents
to other.dollars
and other.cents
so that you can access those attributes for another Money
object.
After changing that:
>>> 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)
Also, you might want to look into the d *= 1
and c *= 100
lines, but that should answer your initial question.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.