我在 python 中制作了一个 class ,它将添加两个数字然后打印是否足够但它没有添加?

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 withdrawn 但似乎我的代码是错误的,但它没有

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

#然后当我尝试执行它时。它只是询问用户但没有解决

  1. 如果要在方法 bankMoney 中打印语句,请删除 return 语句,因为不会执行 return 之后的语句。
  2. 在 if 语句中将 self. 添加到 totalMoney
  3. 调用方法m1.bankMoney执行

尝试以下操作: 代码

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

输出

loaned 1000000
withdrawed 1
 enough money,

像这样:

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.

输出:

loaned : 555.4444
withdrawed: 654654653545.89
enough money,
654654654101.3345

存在三个问题:

  1. 函数未执行
  2. 您的 return 语句缩进不正确
  3. 你不能return打印前,那你根本就无法执行打印语句

试一试:

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