用python计算复利计算器_final = P *(((1 +(r / n))**(n * t)))TypeError:/:'str'和'int'的不支持的操作数类型...

I'm new to Python (running Python 3), have searched every where and still stuck on this.

I understand that I have a string but need to get it into a integer as a final answer. Below is the code I inputted to test it out. I'm stuck on the error that I get.

This is for a compound interest calculator where the user has to input the principal, annual interest rate, how often it's compounded, and the year(s).

I did some research and noticed that i can use ('inf') for p,n,and r as a statement saying either can be large or small numbers. Basically the user can input any number.

P = 10000

n = 12

r = 0.08

p = int(input("Enter starting principle please. "))

n = int(input("Enter Compound intrest rate.(daily, monthly, quarterly, half-year, yearly) "))

r = input("Enter annual interest amount. (decimal) ")

t = int(input("Enter the amount of years. "))

final = P * (((1 + (r/n)) ** (n*t)))

print ("The final amount after", p, "years is", final)

Error:

Enter starting principle please. 75000

Enter Compound intrest rate.(daily, monthly, quarterly, half-year, yearly) 12

Enter annual interest amount. (decimal) 0.03

Enter the amount of years. 15

Traceback (most recent call last):

File "C:\Users\Joshua\AppData\Local\Programs\Python\Python35-32\Program Five.py", line 10, in

final = P * (((1 + (r/n)) ** (n*t)))

TypeError: unsupported operand type(s) for /: 'str' and 'int'

This is my first time using this site, if I left out any information please let me know. I'm open to any suggestions that will help me fix this issue.

Edit

Sorry for the wait. Okay I believe I got it to work correctly going by @Rafael Cardoso recommendation of Stop and Think. Thank you for that.

P = 10000

n = 12

r = 0.08

p = int(input("Enter starting principle please. "))

n = int(input("Enter Compound intrest rate.(daily, monthly, quarterly, half-year, yearly) "))

r = float(input("Enter annual interest amount. (decimal) "))

t = int(input("Enter the amount of years. "))

final = P * (((1 + (r/n)) ** (n*t)))

print ("The final amount after", p, "years is", final)

When I run it, no errors pop up. I found out after taking a breather that the ('inf') was causing the issue, if I leave p,n,r with whats there it works with no issues.

Thanks again every one!

解决方案

According to the stacktrace, r is a str. You should work with numbers only, so in order to do the operation, convert r to float (as it deals with decimal numbers)

r = float(input("Enter annual interest amount. (decimal) "))

Edit

Ok, lets stop and think. As you are using python 3 , the input function will store the value you input as a string (i.e. as text). You want them to be numbers.

So, you have to convert everything to numbers so that you are able to multiply, divide etc. For example, what does "a"/"b" means? And what about "a1"/"b2"? Or even "1"/"2"? They make no sense. However, 1/2 makes sense. The difference is that we are dealing with text in the first 3 examples, and with numbers in the last example.

To make it work, you should convert everything either to int or to float. These types are numbers, and you can do mathematical operations with them without worries.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值