Dozie Daniels
Dozie Daniels

Reputation: 19

One of the methods in my class is working, but another method with the same syntax is not working(python)

class Calculate():

def set_total_cost(self):
total_cost = 1000
self.__total_cost = total_cost

def get_total_cost(self):
return self.__total_cost

def set_down_pmt(self):
down_pmt = 0.25
self.__down_pmt = down_pmt

def get_down_pmt(self):
return self.__down_pmt


test = Calculate()
test.set_total_cost()
test.set_down_pmt()

print(test.get_total_cost())
print(test.get_down_pmt())

The total cost function works, but the get down payment method doesn't, I get this error:

AttributeError: 'Calculate' object has no attribute '_Calculate__down_pmt'

Upvotes: 0

Views: 47

Answers (2)

Dith3r
Dith3r

Reputation: 1

Most likely you have issues with white spaces.

class Calculate():
    def set_total_cost(self):
        total_cost = 1000
        self.__total_cost = total_cost
    def get_total_cost(self):
        return self.__total_cost
    def set_down_pmt(self):
        down_pmt = 0.25
        self.__down_pmt = down_pmt
    def get_down_pmt(self):
        return self.__down_pmt

test = Calculate()
test.set_total_cost()
test.set_down_pmt() 
print(test.get_total_cost())
print(test.get_down_pmt())

Output:

1000

0.25

Upvotes: 0

Ronan Boiteau
Ronan Boiteau

Reputation: 10138

Do not forget that indentation matters in Python!

Here is what your class should look like:

class Calculate():

    def set_total_cost(self):
        total_cost = 1000
        self.__total_cost = total_cost

    def get_total_cost(self):
        return self.__total_cost

    def set_down_pmt(self):
        down_pmt = 0.25
        self.__down_pmt = down_pmt

    def get_down_pmt(self):
        return self.__down_pmt

If the get_down_pmt() method isn't indented, it does not belong to your Calculate class.

Upvotes: 1

Related Questions