Home > front end >  I am making a class in python that would add two numbers then print if its enough or not but It does
I am making a class in python that would add two numbers then print if its enough or not but It does

Time:02-21

#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

CodePudding user response:

  1. Remove the return statement if you want the print statements inside the method bankMoney, as statements after return are not executed.
  2. Add self. to totalMoney at the if statement
  3. Call the method m1.bankMoney to execute it

Try the following: Code

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

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

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

Output

loaned 1000000
withdrawed 1
 enough money,

CodePudding user response:

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

CodePudding user response:

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
  • Related