【问题标题】:Years and months output not formatting properly年月输出格式不正确
【发布时间】: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


【解决方案1】:

在第 8m 行,您的配置说明:

 month_no = math.log(args.payment / (args.payment - i * args.principal), 1 + I)

使用您在问题中陈述的论点,这会产生 23.513122662562726 的值

这就是您在回复中收到1 year and 12 months 的原因,因为技术上是 23 个月,然后四舍五入后转换为 24 个月。

你有两个选择:

  1. 如果业务逻辑规定,最多舍入 24 个月
  2. 删除 math.ceil,并允许将月数显示为分数。

这回答了您的直接问题。

对于长期解决方案,我建议您在日期计算中采用减少分类方法。例如:

month_no = 24 (for your example)
years = math.floor(month_no / 12). -> 2
months = (month_no - (years * 12)) -> 24 - (2 * 12) -> 0

另一个例子:

month_no = 26 (for your example)
years = math.floor(month_no / 12). -> 2
months = (month_no - (years * 12)) -> 26 - (2 * 12) -> 2

从那里你可以使用字符串注入来推送你的打印语句

yr = '{} years'.format(years) if years > 1 else '1 year' if years = 1 else ''
mo = 'and {} months'.format(months) if months > 1 else 'and 1 month' if months = 1 else ''

print('It will take {years}{months} to repay this loan!'.format(years,months))

这是我的例子:

import math
import argparse

#--type=annuity --principal=500000 --payment=23000 --interest=7.8
def no_payments(args):

    i = args['interest'] / (12 * 100)
    month_no = round(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)
    
    print("month no")
    print(month_no, month_count, math.ceil(24 % 12))
    years = math.floor(month_no / 12) #-> 2
    months = (month_no - (years * 12)) # -> 26 - (2 * 12) #-> 2
    yr = '{} years'.format(years) if years > 1 else '1 year' if years == 1 else ''
    mo = 'and {} months'.format(months) if months > 1 else 'and 1 month' if months == 1 else ''
    
    print('It will take {}{} to repay this loan!'.format(yr,mo))

args = {
    'type': 'annuity',
    'principal': 500000,
    'payment': 23000,
    'interest': 7.8
}
no_payments(args)

【讨论】:

  • 啊,我明白了,我没有转换它。谢谢!这让我发疯了。
  • 嘿,rody,你有没有机会检查我的输入作为答案?谢谢!
猜你喜欢
  • 1970-01-01
  • 2013-03-20
  • 1970-01-01
  • 1970-01-01
  • 2015-02-03
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多