I can not understand why the code does not work. Inheritance. Please do not just write the answer, but briefly explain that such questions no longer arise!

class BankAccount: def __init__(self, name, number_count): self.name = name self.number_count = number_count self.count_condition = 0.0 self.valid_count = 328475 def showCount(self, number_count): if self.number_count == self.valid_count: return round(self.count_condition, 1) else: return "Wrong count number!" def addMoney(self, number_count, money_count): if self.number_count == self.valid_count: self.count_condition += money_count else: return "Error!" def minusMoney(self, number_count, money_count): if self.number_count == self.valid_count: self.count_condition -= money_count else: return "Error!" class InterestAccount(BankAccount): def addInterest(self, procent): if self.count_condition > 0: return (BankAccount.count_condition / 100) * self.procent myCount = BankAccount("One", 328475) myCount.addMoney(328475, 45.5) print(myCount.showCount(328475)) myCount.minusMoney(328475, 1.3) print(myCount.showCount(328475)) myProcent = InterestAccount("One", 328475) print(myProcent.addInterest(7)) 

Conclusion :

 45.5 44.2 None 

Closed due to the fact that off-topic participants are Vladimir Martyanov , cheops , Streletz , zRrr , D-side 3 Jun '16 at 12:18 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Vladimir Martyanov, cheops, Streletz, zRrr, D-side
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    And how should it work? - Maxim Timakov
  • must calculate interest InterestAccount (BankAccount) percentage: def addInterest (self, procent): if self.count_condition> 0: return (BankAccount.count_condition / 100) * self.procent and return. And returns None. That is, data from one instance and return to another with changes. - Vadim Vova
  • Try to make the count_condition and valid_count static in the BankAccount class - Maxim Timakov
  • the result is the same - Vadim Vova

2 answers 2

When does a function or method return None? When there is no return, as I understand it, so you None displays. And the condition does not work, because count_condition = 0.0 Because you inherit all the class attributes, but not the attributes of a particular object with changes. Therefore, you should include your init in the child class, that is, redefine it, and then enter it all. Or in a different way (threw off the code):

 class BankAccount: def __init__(self, name, number_count): self.name = name self.number_count = number_count self.count_condition = 0.0 def showCount(self, number_count): if self.number_count == number_count: return round(self.count_condition, 1) else: return "Wrong count number!" def addMoney(self, number_count, money_count): if self.number_count == number_count: self.count_condition += money_count else: return "Error!" def minusMoney(self, number_count, money_count): if self.number_count == number_count: self.count_condition -= money_count else: return "Error!" class InterestAccount(BankAccount): def addInterest(self, procent): if self.count_condition > 0: print('hi') return (self.count_condition / 100) * procent myCount = BankAccount("Daniil", 1) myCount.addMoney(1, 45.5) print(myCount.showCount(1)) myCount.minusMoney(1, 1.3) print(myCount.showCount(1)) myProcent = InterestAccount('Daniil', 1) print(myProcent.count_condition) print(myProcent.addInterest(45.5, 7)) 

    As I understand it, you need something like

     class One: class_var = 0 def modify(self, value): self.__class__.class_var = value class Two(One): def read(self): return self.__class__.class_var one = One() one.modify(42) two = Two() print(two.read()) # 42 

    THOSE. it is not enough to define a field as belonging to a class, it is necessary to refer to it accordingly. Otherwise, the field belonging to self will block access to the class field.

     class One: class_var = 0 def modify(self, value): self.class_var = value class Two(One): def read(self): return self.class_var one = One() one.modify(42) two = Two() print(two.read()) # 0 
    • something like this, but how exactly in my example to get access to self.count_condition, because in your example I don’t quite understand everything - Vadim Vova
    • Something like this: ideone - Maxim Timakov
    • Can you briefly explain what the _class method does ? - Vadim Vova
    • You can read about it, for example, on HabrĂ© - Maxim Timakov