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.