我是编程新手,最近几天一直在努力学习Python。我正在尝试创建一个程序,给定用户生成的输入,该程序可以计算每日利息和每月的贷方利息。
优点:该程序可以根据需要每14天向初始余额中添加存款。该程序能够确定输入是否为a年。该程序能够确定每日利息。
不好:我无法弄清楚如何使程序对给定月份的每日利息进行求和,然后将其添加到下个月的期初余额中。
import datetime
from datetime import date
from datetime import timedelta
import calendar
from decimal import Decimal
def gupta():
print("Enter a start date in YYYY-MM-DD format")
date_entry = input() #records account's start date as string
year, month, day = map(int, date_entry.split('-')) #splits contents of date_entry string variable into separate integers thus rendering date1 variable actionable by datetime.date
date1 = datetime.date(year, month, day) #records account's start date as datetime.date (module?)
print("Enter an end date in YYYY-MM-DD format")
date_entry = input() #records account's end date as string
year, month, day = map(int, date_entry.split('-')) #splits contents of date_entry string variable into separate integers thus rendering date1 variable actionable by datetime.date
date2 = datetime.date(year, month, day) #records account's end date as datetime.date (module?)
print("The starting date is " + str(date1) + " and the ending date is " + str(date2))
print("Enter your initial balance")
initial_balance = Decimal(input()) #requests initial balance (assumes this is amount to be deposited every 14 days)
print("Enter your interest rate (as a decimal)")
interest_rate = Decimal(input()) #records interest rate as a decimal
date1 = date1.toordinal() #changes date1 to ordinal
date2 = date2.toordinal() #changes date2 to ordinal
list_interest = []
for current_date in range(date1, date2 + 1): #cycles through date1 and date2 using a step/iteration of 1
date_spread = current_date - date1
new = current_date + 1
print(date.fromordinal(current_date).strftime('%Y-%m-%d'))
print(list_interest)
if date_spread != 14: #checks to see whether additional deposit is going to be made
current_date = date.fromordinal(current_date)
new = date.fromordinal(new)
current_date_y = str(current_date.strftime("%Y"))
new_m = str(new.strftime("%m"))
current_date_m = str(current_date.strftime("%m"))
current_balance = initial_balance + 0
print(current_balance)
if calendar.isleap(int(current_date_y)):
daily_interest = round(current_balance * (interest_rate / 366), 2)
print(daily_interest)
if current_date_m == new_m:
list_interest.append(daily_interest)
print(list_interest)
else:
daily_interest = round(current_balance * (interest_rate / 365), 2)
print(daily_interest)
if current_date_m == new_m:
list_interest.append(daily_interest)
print(list_interest)
current_date = date.toordinal(current_date)
elif date_spread == 14: #checks to see whether additional deposit is going to be made
current_date = date.fromordinal(current_date)
new = date.fromordinal(new)
current_date_y = str(current_date.strftime("%Y"))
new_m = str(new.strftime("%m"))
current_date_m = str(current_date.strftime("%m"))
current_balance = initial_balance + 150
initial_balance = current_balance
print(current_balance)
if calendar.isleap(int(current_date_y)):
daily_interest = round(current_balance * (interest_rate / 366), 2)
print(daily_interest)
if current_date_m == new_m:
list_interest.append(daily_interest)
print(list_interest)
else:
daily_interest = round(current_balance * (interest_rate / 365), 2)
print(daily_interest)
if current_date_m == new_m:
list_interest.append(daily_interest)
print(list_interest)
current_date = date.toordinal(current_date)
new = date.toordinal(new)
date1 = current_date
list_interest = []
#print(current_balance)