为什么存款后的余额为 None?

huangapple go评论83阅读模式
英文:

why is the balance after depositing is = None?

问题

我创建了这个简单的取款和存款机,并使用类方法进行存款,但似乎出现了问题,因为我期望存款方法的返回值应为20,500,但实际上它是None,所以问题出在哪里?我的代码中有任何错误吗?

print("Hello !!! Welcome to Deposit & Withdrawal Machine")

class Account:
    withDrawalAmount = 0
    depositAmnt = 0

    def __init__(self, ID=0, balance=100, annual_interest_rate=0):
        self.__ID = ID
        self.__balance = balance
        self.__annual_interest_rate = annual_interest_rate

    # setters
    def setId(self, ID):
        self.__ID = ID

    def setBal(self, Bal):
        self.__balance = Bal

    def setAnnualInterestRate(self, annualIntrstRate):
        self.__annual_interest_rate = annualIntrstRate

    # getters
    def getId(self):
        return self.__ID

    def getBal(self):
        return self.__balance

    def getAnnualIntrstRate(self):
        return self.__annual_interest_rate

    # special getters
    def getMonthlyInterestRate(self):
        self.annual_rate = self.__annual_interest_rate / 100
        self.monthly_rate = self.annual_rate / 12
        return self.monthly_rate

    def getMonthlyInterest(self):
        return self.__balance * self.monthly_rate

    # other methods

    def withdraw(self, withDrawalAmount):
        if self.__balance >= withDrawalAmount:
            self.__balance = self.__balance - withDrawalAmount
            return self.__balance

    def deposit(self, depositAmnt):
        if depositAmnt >= 1:
            self.__balance = self.__balance + depositAmnt
            return self.__balance

client001 = Account(1122, 20000, 4.5)

print("Your Balance After withdrawal is : ", client001.withdraw(2500))
print("Your Balance After deposit is : ", client001.deposit(3000))
print("Your Account ID is : ", client001.getId())
print("Your Current Balance is : ", client001.getBal())
print("Your Monthly Intrst Rate is : ", client001.getMonthlyInterestRate())
print("Your Monthly intrst is : ", client001.getMonthlyInterest())

请注意,我已经修改了代码中的几个问题,包括修复了withdrawdeposit方法中的比较条件以及修复了参数的使用。现在,存款方法应该返回正确的值。

英文:

I created this simple withdrawal and deposit machine and used a class method for depositing but there seems to be something wrong with it as I expected the return value of the deposit method to be = 20,500 but instead its = None , so what it is the problem here ? and are there any mistakes in my code ?

print("Hello !!! Welcome to Deposit & Withdrawal Machine")
class Account:
withDrawalAmount = 0 
depositAmnt = 0 
def __init__(self, ID=0, balance=100, annual_interest_rate=0):
self.__ID = ID
self.__balance = balance
self.__annual_interest_rate = annual_interest_rate
#setters
def setId(self, ID):
self.__ID = ID
def setBal(self, Bal):
self.__balance = Bal
def setAnnualInterestRate(self, annualIntrstRate):
self.__annual_interest_rate = annualIntrstRate
#getters
def getId(self):
return self.__ID
def getBal(self):
return self.__balance
def getAnnualIntrstRate(self):
return self.__annual_interest_rate
#special getters 
def getMonthlyInterestRate(self):
self.annual_rate = self.__annual_interest_rate / 100
self.monthly_rate = self.annual_rate / 12
return self.monthly_rate
def getMonthlyInterest(self):
return self.__balance * self.monthly_rate
#other methods
def withdraw(self, withDrawalAmount):
if self.__balance >= self.withDrawalAmount:
self.__balance = self.__balance - self.withDrawalAmount 
return self.__balance
def deposit(self, depositAmnt ):
if self.depositAmnt >=  1 :
self.__balance = self.__balance + self.depositAmnt
return self.__balance
client001 = Account(1122, 20000, 4.5)
print("Your Balance After withdrawal is : ", client001.withdraw(2500))
print("Your Balance After deposit is : ", client001.deposit(3000))
print("Your Account ID is : ", client001.getId())
print("Your Current Balance is : ", client001.getBal())
print("Your Monthly Intrst Rate is : ", client001.getMonthlyInterestRate())
print("Your Monthly intrst is : ", client001.getMonthlyInterest())

答案1

得分: 1

以下是翻译好的内容:

The amount being deposited is depositAmnt, not self.depositAmnt.

So the if condition is false, therefore the function does not return anything, therefore it returns None by default.

英文:
def deposit (self , depositAmnt ):
if self.depositAmnt >=  1 :
self.__balance = self.__balance + self.depositAmnt
return self.__balance

The amount being deposited is depositAmnt, not self.depositAmnt.

So the if condition is false, therefore the function does not return anything, therefore it returns None by default.

答案2

得分: 1

如约翰已经提到的,为了纠正这个问题,你需要在if语句中使用变量 depositAmnt

def deposit(self, depositAmnt):
    if depositAmnt >= 1:
        self.__balance = self.__balance + depositAmnt
        return self.__balance

目前,你的类的属性 - withDrawalAmountdepositAmnt 没有任何用处(因为你将它们作为参数输入到你的 depositwithdraw 函数中)。

你还应该将 withdraw 函数更改为:

def withdraw(self, withDrawalAmount):
    if self.__balance >= withDrawalAmount:
        self.__balance = self.__balance - withDrawalAmount
        return self.__balance
英文:

As John already mentioned, to rectify this you need to use the variable depositAmnt in your if statement.

    def deposit (self , depositAmnt ):
if depositAmnt >=  1 :
self.__balance = self.__balance + depositAmnt
return self.__balance

right now, the attributes of your class -withDrawalAmount and depositAmnt don't have any usage (since you are inputting them as an argument in your function deposit and withdraw.

You should also change the withdraw function as:


def withdraw (self , withDrawalAmount):
if self.__balance >= withDrawalAmount:
self.__balance = self.__balance - withDrawalAmount 
return self.__balance

huangapple
  • 本文由 发表于 2023年2月18日 01:43:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定