[英]Python unsupported operand type(s) for /: 'str' and 'int'
我正在嘗試創建一個程序來計算貸款的每月還款額。 這是我的代碼
#This program will ask for amount of borrow, amount of interest
#, and the number of months of the loan to calculate the payment
print('We are going to caluclate the monthly payment of a loan...\n')
borrowed = input('What is the amount borrow on the loan?')
borrowed = float(borrowed)
interest = input('What is the interest rate on the loan? Enter the percentage: ')
interest = float(interest)
months = input('How long is the loan? Enter in # of months: ')
#calulate the monthly payment
payment = (borrowed*(interest/12)*(1+interest/12)**months)/(((1+interest/12)**months)-1)
payment = format(payment, '.2f')
#print the results
print('\n')
print('The amount of loan is: $', str(borrowed))
print('The interest rate is: ', str(interest) + '%')
print('The length of the loan is: ', str(months) + ' months')
print('The month payment is: $', str(payment))
print('\n')
我一直在第 12 行收到 TypeError: unsupported operand type(s) for ** or pow(): 'float' and 'str'。我不知道如何解決這個問題。 任何幫助都會很棒!
月份應該是一個整數。
#This program will ask for amount of borrow, amount of interest
#, and the number of months of the loan to calculate the payment
print('We are going to caluclate the monthly payment of a loan...\n')
borrowed = input('What is the amount borrow on the loan?')
borrowed = float(borrowed)
interest = input('What is the interest rate on the loan? Enter the percentage: ')
interest = float(interest)
months = input('How long is the loan? Enter in # of months: ')
months = int(months)
#calulate the monthly payment
payment = (borrowed*(interest/12)*(1+interest/12)**months)/(((1+interest/12)**months)-1)
payment = format(payment, '.2f')
#print the results
print('\n')
print('The amount of loan is: $', str(borrowed))
print('The interest rate is: ', str(interest) + '%')
print('The length of the loan is: ', str(months) + ' months')
print('The month payment is: $', str(payment))
print('\n')
你的months
變量是一個字符串。 正如評論之一所說,轉換為 int/number
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.