简体   繁体   中英

Python - What's wrong with my code?

So, i know this is so basic, but i'm getting this error: What can i do to repair this? yy

Traceback (most recent call last): File "/home/coding/Documents/Python Repository/2 - Numbers & math (Loan Calclator).py", line 40, in 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] TypeError: can't multiply sequence by non-int of type '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')

In your line:

    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]

You are using square brackets (in this case used to create a list) as a means of specifying order of operations. This should be () type brackets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM