Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 年和月输出格式不正确_Python_Python 3.x_Datetime_Time - Fatal编程技术网

Python 年和月输出格式不正确

Python 年和月输出格式不正确,python,python-3.x,datetime,time,Python,Python 3.x,Datetime,Time,当我尝试在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_ar

当我尝试在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()
导入数学
导入argparse
parse=argparse.ArgumentParser(用法=“计算每月递减付款的差异计算器”
“本金金额”)
parse.add_参数('--type','-t',type=str,required=True,help='diff=differential,annuity=standard,fixed payments')#错误代码
#选择diff参数时无法计算,因为每个月的付款不同
parse.add_参数('--payment','-p',type=float,required=False,help='monthly payment amount')
parse.add_参数('--principal','-P',type=float,required=False,help='principal-amountoftheloan')
parse.add_参数('--periods','-m',type=int,required=False,help='支付贷款所需的付款次数')
parse.add_参数('--interest','-i',type=float,required=True,help='interest rate(作为整数,未转换)')#错误代码
args=parse.parse_args()
def无需付款():
i=参数利息/(12*100)
月号=数学日志(args.payment/(args.payment-i*args.principal),1+i)
超额支付=args.本金*args.利息
年份计数=数学单元(第12个月)
月份计数=math.ceil(月份编号%12)
如果1<第1个月:
打印(f'偿还这笔贷款需要{年数}年和{月数}个月!')
elif月号>=24,月数==1:
打印(f'偿还这笔贷款需要{年数}年和{月数}月!')
如果月数>=24且月数>1:
打印(f'偿还这笔贷款需要{年数}年和{月数}月!')
打印(f'Overpayment={Overpayment}')
#如果未输入兴趣和类型,则抛出错误代码
如果args.interest为无:
打印('参数不正确')
elif args.type为无:
打印('参数不正确')
如果args.type==“年金”且args.payment为无:
安_calc()
elif args.type=='diff',args.payment为无:
差异计算()
elif args.type==“年金”且args.principal为无:
普林斯()
elif args.type==“年金”且args.periods为无:
无需付款()
给定参数--type=年金--本金=500000--payment=23000--interest=7.8.

产量应该是2年,但结果是1年12个月。要使输出为2年,我必须更改什么?

在第8m行,您的配置状态为:

 month_no = math.log(args.payment / (args.payment - i * args.principal), 1 + I)
使用您在问题中陈述的参数,得出的值为
23.513122662562726

这就是您在回复中收到
1年12个月的原因,因为技术上是23个月,四舍五入时转换为24个月

您有两个选择:

  • 如果业务逻辑规定,则最长为24个月
  • 删除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
    
    另一个例子:

    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)
    

    我想你这里缺少一些代码。在代码中有变量“overpayment”,在函数中有args。您能更新您的代码以包含一个示例调用吗?例如,设置args值的东西?THanks@arcee123我更新了代码并添加了参数(如果有帮助的话)。由于stackoverflow不允许,我无法放入完整代码。啊,我明白了,我没有转换它。非常感谢。我快发疯了。嘿,罗迪,你有没有可能检查一下我的输入作为答案?谢谢