我得到的指导原则是,所有IO都应包含在主函数“ def main()”中。我需要4个输出,“ Loan Amount”和“付款数”来自主函数中的输入,而我在“按月付款”和“利息”方面苦苦挣扎的两个输出是在主函数之外计算的。我尝试使用return,并在打印描述符中尝试引用它们,但出现错误,该变量未定义。我有的解决方案打印两列,一列来自主函数,另一列来自借贷函数。
#libraries
import sys
#Interest is monthly apr --> interest = (rate / 100) / 12
#Monthly Pay formula is apr formula split into variables numerator and denominator
#functions
def small_loan(lend_1, term_1): #lend_1 is loan parameter while term_1 is finance term perameter
'''monthly payment formula is A = P * (rate) * ((1 + (rate)) ** term) / ((1 + (rate)) ** term) - 1
P == lend_x term == term_x rate == interest ***This is for all calculation functions***
'''
#mon_pay = numerator / denominator
if term_1 >= 6 and term_1 <= 12:
rate = 8
interest = (rate / 100) / 12
numerator = lend_1 * interest * ((1 + interest) ** term_1)
denominator = ((1 + interest) ** term_1) - 1
mon_pay = numerator / denominator
print("Monthly Payment: $%3.2f Interest:%2d percent" % (mon_pay, rate))
elif term_1 >= 13 and term_1 <= 36:
rate = 10
interest = (rate / 100) / 12
numerator = lend_1 * interest * ((1 + interest) ** term_1)
denominator = ((1 + interest) ** term_1) - 1
mon_pay = numerator / denominator
print("Monthly Payment: $%3.2f Interest:%2d percent" % (mon_pay, rate))
elif term_1 >= 37 and term_1 <= 48:
rate = 12
interest = (rate / 100) / 12
numerator = lend_1 * interest * ((1 + interest) ** term_1)
denominator = ((1 + interest) ** term_1) - 1
mon_pay = numerator / denominator
print("Monthly Payment: $%3.2f Interest:%2d percent" % (mon_pay, rate))
#func call to loop to continue or end program via sys.exit
loop()
def med_loan(lend_2, term_2): #lend_2 is the loan perameter while term_2 is the finacial term peramter
'''monthly payment formula is A = P * (rate) * ((1 + (rate)) ** term) / ((1 + (rate)) ** term) - 1
P == lend_x term == term_x rate == interest ***This is for all calculation functions***
'''
#mon_pay = numerator / denominator
if term_2 >= 6 and term_2 <= 12:
rate = 7
interest = (rate / 100) / 12
numerator = lend_2 * interest * ((1 + interest) ** term_2)
denominator = ((1 + interest) ** term_2) - 1
mon_pay = numerator / denominator
print("Monthly Payment: $%3.2f Interest:%2d percent" % (mon_pay, rate))
elif term_2 >= 13 and term_2 <= 36:
rate = 8
interest = (rate / 100) / 12
numerator = lend_2 * interest * ((1 + interest) ** term_2)
denominator = ((1 + interest) ** term_2) - 1
mon_pay = numerator / denominator
print("Monthly Payment: $%3.2f Interest:%2d percent" % (mon_pay, rate))
elif term_2 >= 37 and term_2 <= 48:
rate = 6
interest = (rate / 100) / 12
numerator = lend_2 * interest * ((1 + interest) ** term_2)
denominator = ((1 + interest) ** term_2) - 1
mon_pay = numerator / denominator
print("Monthly Payment: $%3.2f Interest:%2d percent" % (mon_pay, rate))
#func call to loop to continue or end program via sys.exit
loop()
def large_loan(lend_3, term_3): #lend_3 is the loan perameter while term_3 is the finacial term peramter
'''monthly payment formula is A = P * (rate) * ((1 + (rate)) ** term) / ((1 + (rate)) ** term) - 1
P == lend_x term == term_x rate == interest ***This is for all calculation functions***
'''
#mon_pay = numerator / denominator
if term_3 >= 6 and term_3 <= 12:
rate = 5
interest = (rate / 100) / 12
numerator = lend_2 * interest * ((1 + interest) ** term_2)
denominator = ((1 + interest) ** term_2) - 1
mon_pay = numerator / denominator
print("Monthly Payment: $%3.2f Interest:%2d percent" % (mon_pay, rate))
elif term_3 >= 13 and term_3 <= 36:
rate = 6
interest = (rate / 100) / 12
numerator = lend_2 * interest * ((1 + interest) ** term_2)
denominator = ((1 + interest) ** term_2) - 1
mon_pay = numerator / denominator
print("Monthly Payment: $%3.2f Interest:%2d percent" % (mon_pay, rate))
elif term_3 >= 37 and term_3 <= 48:
rate = 7
interest = (rate / 100) / 12
numerator = lend_2 * interest * ((1 + interest) ** term_2)
denominator = ((1 + interest) ** term_2) - 1
mon_pay = numerator / denominator
print("Monthly Payment: $%3.2f Interest:%2d percent" % (mon_pay, rate))
#func call to loop to continue or end program via sys.exit
loop()
def loop():
cont = input("Would you like to continute? 'Y for yes and N for no' ")
while cont == "Y" or cont == "y":
main()
if cont == "N" or cont == "n":
print("You have chosen to exit the program. Thank you and have a nice day!")
sys.exit(0)
#main()
def main():
while True:
try:
loan = input("How much money will you be financing? ")
loan = float(loan)
if loan < 500:
print("We do not give our loans less than $500")
main()
payment = input("How many monthly payments do you want? ")
payment = int(payment)
break
if payment < 6 and payment > 48:
print("Sorry we do not finance longer than loan less than 6 months or longer than 48 months")
main()
except ValueError:
print("Please input a numbers only")
main()
if loan >= 500 and loan <= 2500:
print("Loan Amount: $%4.2f Number of Payments: %2d" % (loan, payment))
small_loan(loan, payment)
elif loan >= 2501 and loan <= 10000:
print("Loan Amount: $%4.2f Number of Payments: %2d" % (loan, payment))
med_loan(loan, payment)
elif loan >= 10001:
print("Loan Amount: $%4.2f Number of Payments: %2d" % (loan, payment))
large_loan(loan, payment)
#call to main
main()