I have a simple class and it doesn’t answer when I call the methods

Asked

Viewed 43 times

0

Can someone help me?

class sorvete():
    def __init__(self,sabor, recipiente):
        self.sabor= sabor
        self.recipiente= recipiente
        self.ml= 0

    def sabor(self):
        print("O sabor que pediu é: ", (self.sabor))
        
    def recipiente(self):
        print("O recipiente que pediu é: ", (self.recipiente))
        

    def quantidade(self,ml):
        self.ml= self.ml + ml

sorvete1=sorvete('Morango','Casquinha')
print(sorvete1)
print(sorvete1.sabor())
print(sorvete1.recipiente())
print(sorvete1.quantidade(500))

Error:

Typeerror: 'str' Object is not callable

  • 2

    You have defined a method with the same name as an attribute. In this case, when you call sorvete1.sabor() Python searches for the attribute that is string. Rename one of the two.

  • Adding to the @Paulomarques comment, you can access the attributes this way sorvete1.sabor and sorvete1.recipiente See more here

  • It worked!! I thank you very much

No answers

Browser other questions tagged

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