'I am making a class in python that would add two numbers then print if its enough or not but It doesn't add?

#this is my code that would add loaned and withdrawed but it seems like my code is wrong but it doesn't have

class Money:
    loaned = 0
    withdrawed = 0
    totalMoney = 0

    def bankMoney (self):
        self.totalMoney = self.withdrawed + self.loaned
            return totalMoney
        if totalMoney >= 1000000:
            print(" enough money,")
        else: 
            print("not enough")

m1 = Money()
m1.loaned = float(input("loaned"))
m1.withdrawed = float(input("withdrawed"))

#then when I try to execute it. it just ask the user but doesn't solve



Solution 1:[1]

There are three issues:

  1. the function is not executed
  2. your return statement is not indented correctly
  3. you can't return before print, then you won't be able to execute the print statement at all

try it:

class Money:
    def __init__(self):
        self.totalMoney = 1000000
        self.loaned = float(input("loaned: "))
        self.withdrawed =  float(input("withdrawed: "))

    def bankMoney (self):
        total = self.withdrawed + self.loaned
        if total >= self.totalMoney:
            print(" enough money")
        else:
            print("not enough")
        return total


m1 = Money()
m1.bankMoney()

Solution 2:[2]

Like this:

class Money():
    def __init__(self,l, w):
        self.loaned = l
        self.withdrawed = w
        self.totalMoney = 0
    def bankMoney(self):
        self.totalMoney = self.withdrawed + self.loaned
        if self.totalMoney >= 1000000:
            print("enough money,")
        else: 
            print("not enough")
        return self.totalMoney # this is returned at the end

loaned = float(input("loaned : "))
withdrawed = float(input("withdrawed: "))
m1 = Money(loaned, withdrawed)
print(m1.bankMoney()) # the method within the class is to be called.

Output:

loaned : 555.4444
withdrawed: 654654653545.89
enough money,
654654654101.3345

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 maya
Solution 2