对于Python's,不支持str/'和'int'类型

2024-09-27 07:17:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个程序来计算每月的贷款支付。这是我的密码

#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')

我一直得到TypeError:不支持**或pow()的操作数类型:第12行的“float”和“str”。我不知道怎么解决这个问题。任何帮助都将是伟大的!在


Tags: oftheinputispaymentfloatamountprint
2条回答

您的months变量是一个字符串。正如其中一条评论所说,转换成int/number

月份应为整数。在

#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')

相关问题 更多 >

    热门问题