vimeo / py-money

Money class for Python 3
MIT License
125 stars 27 forks source link

money.exceptions.InvalidOperandError: Invalid operand types for operation #4

Closed francesco1119 closed 6 years ago

francesco1119 commented 6 years ago

I have my spare code:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from money.money import Money
from money.currency import Currency                         
x = Money('0.01', Currency.GBP)

words = 'alphabet'

for i in words:
    x += 0.01

print (x)

But when I run it it returns me:

Traceback (most recent call last):
  File "test.py", line 11, in <module>
    x += 0.01
  File "C:\Program Files\Python36\lib\site-packages\money\money.py", line 81, in __add__
    raise InvalidOperandError
money.exceptions.InvalidOperandError: Invalid operand types for operation

I believe += is the problem

nickyr commented 6 years ago

The problem is that you are trying to add a float to a Money, but you must add a Money to a Money. Additionally, the Currencies of the two Monies must match. So you should use:

x += Money('0.01', Currency.GBP)