Python 如何每月向银行账户余额增加一次利息?
我必须创建一个bankaccount类,该类具有如下功能:存款、取款、获取余额;所有那些初学者的东西。一切都很容易,我完成了它没有问题。然而,有一个叫做利息的函数,它接受一个利率,我遇到了很多麻烦。为了说明这一点,该功能应该每月仅根据用户的请求向银行账户余额添加一次利息。假设我决定在今天(10月13日)的余额上加5%的利息。该函数将执行并吐出我的新余额。然后,假设我想在第二天(10月14日)增加10%的利息,该函数将不会执行,并会抛出类似“11月1日之前不能增加利息”的内容,这是下个月的第一天。在11月1日,如果我尝试增加10%的利息,我会成功,然后在11月2日,如果我再次尝试增加利息,它会说“12月1日之前不能增加利息”等等。这件事让我很难受。下面是我写的代码,但是它不仅会执行,因为日期总是提前一个月,而且它也总是会给余额增加利息 这是我到目前为止所拥有的,但这是一片混乱。有人知道我该怎么做吗?我知道它不在函数中,但我想我会先弄清楚程序的主要内容,然后再考虑把它融入到我的课堂中Python 如何每月向银行账户余额增加一次利息?,python,datetime,relativedelta,Python,Datetime,Relativedelta,我必须创建一个bankaccount类,该类具有如下功能:存款、取款、获取余额;所有那些初学者的东西。一切都很容易,我完成了它没有问题。然而,有一个叫做利息的函数,它接受一个利率,我遇到了很多麻烦。为了说明这一点,该功能应该每月仅根据用户的请求向银行账户余额添加一次利息。假设我决定在今天(10月13日)的余额上加5%的利息。该函数将执行并吐出我的新余额。然后,假设我想在第二天(10月14日)增加10%的利息,该函数将不会执行,并会抛出类似“11月1日之前不能增加利息”的内容,这是下个月的第一天。
from datetime import *
from dateutil.relativedelta import *
rate = 0.10
balance = 1000.00 # i just made up a number for the sake of the code but the
# balance would come from the class itself
balancewInterest = rate*balance
print(balancewInterest)
# this is where I'm having the most trouble
todayDate = date.today()
print(todayDate)
todayDay = todayDate.day
todayMonth = todayDate.month
if todayDay == 1: # if it's the first of the month, just change the month
# not the day
nextMonth = todayDate + relativedelta(months=+1)
else: # if not the 1st of the month, change day to 1, and add month
nextMonth = todayDate + relativedelta(months=+1, days=-(todayDay-1))
print(nextMonth)
difference = (todayDate - nextMonth)
print(difference.days)
if todayDate >= nextMonth: # add interest only when it's the first of the next
# month or greater
interestPaid = rate*balance
print(interestPaid)
else:
print("You can add interest again in " + str(abs(difference.days)) \
+ " days.")
我现在已经完成了怎么做
import datetime
import os
import time
x = datetime.datetime.now()
try:
s = open("Months.txt","r")
ss = s.read()
s.close()
except IOError:
s = open("Months.txt","w")
s.write("Hello:")
s.close()
try:
Mo = open("Deposit and interest.txt","r")
Mon = Mo.read()
Mo.close()
except IOError:
Deposit = input("How much do you wish to deposit")
M = open("Deposit and interest.txt","w")
M.write(Deposit)
M.close()
s1 = open("Months.txt","r")
s2 = s1.read()
s1.close()
Mone = open("Deposit and interest.txt","r")
Money = Mone.read()
Mone.close()
if s2 != x.strftime("%B"):
Rate = input("How much interest do you want?")
Deposit1 = int(Money)/int(100)
Deposit2 = Deposit1*int(Rate)
Deposit = int(Money) + int(Deposit2)
print("Calculation of interest")
time.sleep(2)
print("The final total is: "+str(Deposit))
os.remove("Months.txt")
file_one=open("Months.txt","w")
file_one.write(x.strftime("%B"))
file_one.close()
os.remove("Deposit and interest.txt")
file_one=open("Deposit and interest.txt","w")
file_one.write(str(Deposit))
file_one.close()
print("Used up this month, try again next month")
else:
print("Sorry, you have used this months intrest limit, try again next month")
这不是一个类,你在问题中提到你创建了一个BankAccount类,你能把它添加到问题中吗?我对这个类没有问题,我对增加余额兴趣的特定函数有问题。你问题中暴露的规范不完全清楚,但是如果你想让你的程序以这种方式工作,你需要将兴趣添加了一个月的信息存储在某个地方。这应该在类中,或者在某个文件/db中,如果你想让它在程序停止并再次启动时起作用的话。我认为计算是:balancewInterest=(利率*余额)+balance我从SO学到了更多关于计算月利息的知识,而不是编码:SOP对微积分的基础知识不感兴趣。阅读他们的问题:他们想要的是一种每月做一次的方法。这就是问题所在。
def calculate_interest(amount_deposited, time):
rate_of_interest = 5
int rest = (amount_deposited*time*rate)
return int rest
print(calculate_interest(1000, 2))
''' for eg: here 1000 is the amount deposited and for 2 years at the rate of interest 5.'''