1
As an apprenticeship of Property, created a small currency converter. See:
class converteMoeda():
def __init__(self, dollar=0, real=0):
self.dollar = dollar
self.real = real
def toDollar(self):
dollar = self.real/self.dollar
print("Convertendo Reais para Dólar.")
return(dollar)
def toReais(self):
reais = self.dollar/self.reais
print("Convertendo de Dólar para Reais.")
return(reais)
def setDollar(self,dollar):
print("Gravando valor do dólar.")
self._dollar = dollar
def setReais(self,real):
print("Gravando valor do real.")
self._real = real
def getDollar(self):
print("Recuperando valor do dólar.")
return(_dollar)
def getReais(self):
print("Recuperando valor do Real.")
reutrn(_real)
real = property(getReais,toReais)
dollar = property(getDollar,toDollar)
c = converteMoeda()
c.toReais(3.17)
However, for a reason that I could not identify, I am generating an error in the number of parameters:
Traceback (most recent call last):
File "python", line 35, in <module>
File "python", line 5, in __init__
TypeError: toDollar() takes 1 positional argument but 2 were given
Could someone help me on this adventure?
The method
toDollar
only receivesself
as a parameter. If it functions as a Setter property, it must receive the new property value as parameter as well.– Woss
By the way, your code has several syntax errors.
– Woss