Why can’t I pass a parameter to the class?

Asked

Viewed 244 times

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".

1 answer

2


It lacks a little logic in its code, in addition to having a certain error.

First of all, you’re not getting tinta as parameter, and this is not even necessary once the value is already saved in the object. You are receiving self as parameter (which is the object itself). So instead of trying to access directly tinta, you need to access the property (or attribute) tinta that is within the parameter (also called tinta, in this case - I changed the name to self just to avoid confusion).

Second, this loop is completely unnecessary. You don’t need to make a loop to find out if the ink level is in a certain range, just use the logical comparators < (less than) and > (greater than).

Another important thing, even if the loop be necessary, the instruction break with the code "jump out of the loop", this means that the conditions will be assessed only once and after that the loop is stopped.

I made an adaptation to your code and now it’s functional.

class Caneta():

    acao = 'escrever'

    def __init__(self, tinta):
        self.tinta = tinta

    def NivelTinta(self):
        print(self.tinta)

        if(1 <= self.tinta <= 5):
            print('A caneta esta falhando')
        elif(5 < self.tinta <= 100):
            print('A caneta esta boa')
        else:
            print('A tinta acabou')

CanetaAzul = Caneta(50)
CanetaAzul.NivelTinta()
  • 1

    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 :)

  • 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].

  • 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".

  • It turns out that in the method init you receive the value ink and assign ("injects") it as parameter value within self (which is the object caneta). There you can use any name, do a test and change the parameter name to t. And change the second line to self.tinta = t, behavior will remain the same. In the second method you are using the value tinta that exists inside the object, the variable used in the init no longer exists. And one more detail, you are attributing tinta for self.tinta and not equalizing.

  • 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). :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.