final = P * (((1 + (r/n)) ** (n*t))) TypeError: unsupported operand type(s) for /: 'str' and 'int'

北慕城南 提交于 2020-01-06 19:58:44

问题


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 <module>
    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!


回答1:


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.




回答2:


You can use the module decimal:

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


来源:https://stackoverflow.com/questions/34948946/final-p-1-r-n-nt-typeerror-unsupported-operand-types-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!