3
I’m trying to pass the parameter "ink" to the class "Pen" but I can’t. I’ve tried using boolean expressions instead of index, but still no result. Could someone give me a help?
class Caneta():
açao = 'escrever'
def __init__(self, tinta):
self.tinta = tinta
def NivelTinta(tinta):
rng = range(0,100)
for tinta in rng:
if tinta in rng[5:100]:
print('A caneta está boa')
break
elif tinta in rng[1:4]:
print('A caneta está falhando')
break
else:
print('A tinta acabou')
break
CanetaAzul = Caneta(50)
CanetaAzul.NivelTinta()
Every time I run the code appears "The ink is gone", and should appear "The pen is good", because the parameter I put is "50".
Thank you very much, you helped so much! When I was trying the Boolean expression code that was one of the things I was trying to do: concatenate two Boolean operators, only I was using "and", so it wasn’t working out so well. That "self" explanation of yours made a lot of things clear too :)
– Nelthar
Show! Always good to help. You can yes use the
and
, but I wouldn’t have to, since Python (♥
) gives this facilitated for us. Welcome to [pt.so].– Jéf Bueno
Thank you! Just one thing: It occurred to me a question now of why I have to match "self.tinta" and "tinta" in the method init. Since in the "Niveltinta" method I used "self.tinta" and not "tinta".
– Nelthar
It turns out that in the method
init
you receive the value ink and assign ("injects") it as parameter value withinself
(which is the objectcaneta
). There you can use any name, do a test and change the parameter name tot
. And change the second line toself.tinta = t
, behavior will remain the same. In the second method you are using the valuetinta
that exists inside the object, the variable used in theinit
no longer exists. And one more detail, you are attributingtinta
forself.tinta
and not equalizing.– Jéf Bueno
I think I understand, so the function of this attribution is because the parameters of the init method expect a value for the same power to "work" (let’s say so). :)
– Nelthar