【问题标题】:Python - What's wrong with my code?Python - 我的代码有什么问题?
【发布时间】:2016-06-17 00:18:16
【问题描述】:

所以,我知道这是非常基本的,但我收到了这个错误: 我能做些什么来修复这个? y.y

Traceback(最近一次调用最后一次):文件 “/home/coding/Documents/Python Repository/2 - 数字和数学(贷款 Calculator).py",第 40 行,在 每月付款=浮动(贷款金额)* [0.05 *(1.0 + 0.05)*浮动(支付数量)] / [(1.0 + 0.05)*浮动(支付数量)- 1.0] TypeError:不能将序列乘以'float'类型的非整数

#Loan Calculator

#Monthly Payments formula > M = L[i(1+i)n] / [(1+i)n-1]
# M = Month Payment
# L = Loan amount
# i = interest rate (int rate of 5%, i = 0.05)
# n = number of payments


print ('Hey there! Welcome to Johnny\'s Loan Bank!')

#just set variable
monthly_payment = 0

#Set how much money user will need
loan_input = input('So, how much would you like to lend? ')

#transform the input in a float
loan_amount = float(loan_input)


#Set number of months to pay
num_of_pay_input = input('And in how many months would you like to pay? ')

#transform value into a float
num_of_payments = float(num_of_pay_input)

print ('\nSo, you want to lend {0:.1f} in {1:.1f} payments'.format(loan_amount, num_of_payments))
print ('You should know that we work with an interest rate of 5%')

#Variable set to know the users decision
user_decision = input('Would you like to continue?: ')
print (user_decision)

if user_decision == 'y' or decision == 'yes':
    print ('loan_amount {0:.1f}'.format(loan_amount))
    print ('num_of_payments {0:.1f}'.format(num_of_payments))

Line 40>>>>     monthly_payment = float(loan_amount) * [0.05 * (1.0 + 0.05) * float(num_of_payments)] / [(1.0 + 0.05) * float(num_of_payments) - 1.0]

    print ('Ok, you\'ll get a loan of {0:.1f} and you gonna pay us in {1:.1f} months'.format(loan_amount, num_of_payments) + \
        ', thats gonna be {0:.1f} for month.'.format(monthly_payment))
else:
    print ('get off my bank')

【问题讨论】:

  • 代码[0.05 * (1.0 + 0.05) * float(num_of_payments)] 正在创建一个长度为1 的列表,其中包含一个浮点数,您正试图将其乘以一个浮点数。你的意思可能是(0.05 * (1.0 + 0.05) * float(num_of_payments)),它只会创建一个浮点数
  • 天啊哈哈哈我知道这很简单。非常感谢。我开始编码几天,这是我没有意识到的。非常感谢您的帮助,谢谢!

标签: python python-2.7 python-3.x


【解决方案1】:

在你的行中:

    monthly_payment = float(loan_amount) * [0.05 * (1.0 + 0.05) * float(num_of_payments)] / [(1.0 + 0.05) * float(num_of_payments) - 1.0]

您正在使用方括号(在本例中用于创建列表)作为指定操作顺序的一种方式。这应该是() 类型的括号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-02
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多