Python ATM,简单减法的问题?

时间:2018-04-23 05:52:06

标签: python

这是我第一次使用Python类的实验,不知道为什么它不能从余额中提取(减去)。知道我做错了吗?

# ATM class with all functions
class ATM:
    def __init__(self, balance, interest=0.1):
        self.balance = balance
        self.interest = interest
        self.transactions = []

    def check_balance(self):  # checks balance
        return f"Your balance is: ${self.balance}"

    def deposit(self, amount):  # deposits to balance
        self.balance += amount
        print(f"Deposit amount: {amount}")
        return self.balance

    def check_withdrawal(self, amount):  # checks balance to see if we can withdraw
        if self.balance - amount > 0:
            return True
        else:
            print('Not enough funds to withdraw.')

    def withdraw(self, amount):  # *withdraws $$$$$$$$*
        if self.balance - amount:
            return f"You have withdrawn ${amount}"


# testing
checking_account = ATM(50000)
print(checking_account.check_balance())
print(checking_account.check_withdrawal(900))
print(checking_account.withdraw(900))
print(checking_account.check_balance())

1 个答案:

答案 0 :(得分:1)

def withdraw(self, amount):  # *withdraws $$$$$$$$*
        if self.balance - amount:
            self.balance = self.balance - amount
            return f"You have withdrawn ${amount}"

更改您的提款功能,因为您没有将扣除的金额保存到余额