【发布时间】:2020-12-09 04:41:59
【问题描述】:
当我尝试在我的 python 脚本中实现 no_payment() 这个函数时:
import math
import argparse
parse = argparse.ArgumentParser(usage='Differential calculator that calculates the per month payment on a decreasing'
'principal amount')
parse.add_argument('--type', '-t', type=str, required=True, help='diff = differential, annuity = standard, fixed payments') #error code
# cant compute with diff argument selected, as payment each month is different
parse.add_argument('--payment', '-p', type=float, required=False, help='monthly payment amount')
parse.add_argument('--principal', '-P', type=float, required=False, help='principal amount of the loan')
parse.add_argument('--periods', '-m', type=int, required=False, help='number of payments required to pay the loan')
parse.add_argument('--interest', '-i', type=float, required=True, help='interest rate (as integer, not converted)') #error code
args = parse.parse_args()
def no_payments():
i = args.interest / (12 * 100)
month_no = math.log(args.payment / (args.payment - i * args.principal), 1 + i)
overpayment = args.principal * args.interest
year_count = math.ceil(month_no // 12)
month_count = math.ceil(month_no % 12)
if 1 < month_no <= 12:
print(f'It will take {month_count} months to repay this loan!')
elif month_no == 1:
print(f'It will take 1 month to repay this loan!')
elif month_no == 12:
print(f'It will take 1 year to repay this loan!')
elif 12 < month_no < 24 and month_count == 1:
print(f'It will take {year_count} year and 1 month to repay this loan!')
elif 12 < month_no < 24 and month_count > 1:
print(f'It will take {year_count} year and {month_count} months to repay this loan!')
elif month_no >= 24 and month_count == 1:
print(f'It will take {year_count} years and {month_count} month to repay this loan!')
elif month_no >= 24 and month_count > 1:
print(f'It will take {year_count} years and {month_count} months to repay this loan!')
print(f'Overpayment = {overpayment}')
# error codes thrown if interest and type are not inputted
if args.interest is None:
print('Incorrect Parameters')
elif args.type is None:
print('Incorrect Parameters')
if args.type == 'annuity' and args.payment is None:
ann_calc()
elif args.type == 'diff' and args.payment is None:
diff_calc()
elif args.type == 'annuity' and args.principal is None:
princ()
elif args.type == 'annuity' and args.periods is None:
no_payments()
使用给定参数 --type=annuity --principal=500000 --payment=23000 --interest=7.8。
输出应该是 2 年,但结果是 1 年零 12 个月。我必须改变什么才能使输出为 2 年?
【问题讨论】:
-
我认为您在这里遗漏了一些代码。在代码中,您有变量“多付”,而在函数中,您有 args。您能否更新您的代码以包含示例调用?例如,设置 args 值的东西?谢谢
-
@arcee123 如果有帮助,我更新了代码并添加了参数。我不能把我的完整代码,因为 stackoverflow 不允许我这样做。
标签: python python-3.x datetime time