【问题标题】:Basic mortgage calculator基本抵押贷款计算器
【发布时间】:2015-09-01 21:58:44
【问题描述】:

所以我是一个初学者,我正在尝试制作一个简单的抵押贷款计算器。这是我的代码:

L=input('Enter desired Loan amount: ')
I=input('Enter Interest Rate: ')
N=input('Enter time length of loan in months: ')

MonthlyPayments= [float(L)*float(I)*(1+float(I))*float(N)]/((1+float(I))*float(N)-1)


print('Your Monthly Payments will be {0:.2f}'.format(MonthlyPayments))`

我收到一条错误消息:不支持的操作数类型 for /: 'list' and 'float'

【问题讨论】:

    标签: python list calculator


    【解决方案1】:

    首先使用方括号会创建一个可能不是您想要的列表。此外,为了避免必须不断转换,您可以(并且应该)使用您期望获得的类型包装您的输入调用。

    所以从你的示例代码开始,我会这样写:

    L=float(input('Enter desired Loan amount: '))
    I=float(input('Enter Interest Rate: '))
    N=float(input('Enter time length of loan in months: '))
    
    MonthlyPayments = (L*I*(1+I)*N)/((1+I)*N-1)
    
    print('Your Monthly Payments will be {0:.2f}'.format(MonthlyPayments))
    

    这也使阅读更容易

    【讨论】:

      【解决方案2】:
       MonthlyPayments= (float(L)*float(I)*(1+float(I))*float(N))/((1+float(I))*float(N)-1)
      

      '[' 和 ']' 创建一个列表。

      【讨论】:

        【解决方案3】:

        这里:

        MonthlyPayments = [float(L)*float(I)*(1+float(I))*float(N)]/((1+float(I))*float(N)-1)
        

        这部分:

        [float(L)*float(I)*(1+float(I))*float(N)]
        

        提供“列表”数据类型。将[] 替换为()

        【讨论】:

        • 感谢您的帮助!
        猜你喜欢
        • 2023-04-05
        • 1970-01-01
        • 2012-05-31
        • 1970-01-01
        • 2020-12-13
        • 2015-06-30
        • 1970-01-01
        • 2013-06-10
        • 1970-01-01
        相关资源
        最近更新 更多