I need the sum of all the outputs of Dm, but cannot use sum because it is not iterable. How do I go about getting the sum of all outputs?
def diff_calc():
# create alias for all parsed arguments for cleaner code
loan_type = args.type
payment = args.payment
principal = args.Principal
periods = args.periods
interest = args.interest
if args.interest is None:
interest = 10
if args.Principal is None:
principal = 1000000
if args.periods is None:
periods = 10
i = (interest / (12 * 100))
for m in range(1, periods + 1, 1):
Dm = ((principal / periods) + i * (principal - (principal * (m - 1)) / periods))
print(f'Month {m}: payment is {math.ceil(Dm)}')
Output is:
I need to add those number up and subtract my principal from that for the overpayment amount.
You can either store those values in a dictionary, and then manipulate it however you want:
monthly_payments = {}
for m in range(periods):
Dm = some_calculation...
monthly_payments['month_' + str(m)] = Dm
yearly_payments = sum(monthly_payments.value())
You can also just add each calculation of Dm to some variable to hold the total sum.
total = 0
for m in range(periods):
Dm = some_calculation...
total += Dm
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.