final = P *((((1 +(r/n))**(n * t))))TypeError:/不支持的操作数类型:' str'和' int' [英] final = P * (((1 + (r/n)) ** (n*t))) TypeError: unsupported operand type(s) for /: 'str' and 'int'
问题描述
我是Python的新手(运行Python 3),已经搜索了所有位置,但仍然停留在此位置.我知道我有一个字符串,但需要将其转换为整数作为最终答案.下面是我输入的用于测试的代码.我被卡在我得到的错误上.这是用于复利计算器的,用户必须输入本金,年利率,复利频率和年份.我进行了一些研究,发现我可以对p,n和r使用('inf')作为声明,说可以是大数或小数.基本上,用户可以输入任何数字.
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)
错误:
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
抱歉,请耐心等待.好的,我相信通过Stop and Think的@Rafael Cardoso建议,它可以正常工作.谢谢你.
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)
当我运行它时,没有错误弹出.我喘口气后才发现是('inf')
引起问题的原因,如果我将p,n,r留在原处,那没有问题.
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.
再次感谢大家!
推荐答案
根据堆栈跟踪, r
是 str
.您应该只使用数字,因此为了进行操作,请将r转换为 float
(因为它处理十进制数字)
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) "))
编辑
好吧,让我们停下来思考.当您使用 python 3
时,输入函数会将您输入的值存储为字符串(即文本).您希望它们成为数字.
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.
因此,您必须将所有内容都转换为数字,以便能够相乘,除等.例如,"a"/"b"
是什么意思?那么"a1"/"b2"
呢?甚至是"1"/"2"
?他们没有道理.但是, 1/2
是有意义的.区别在于,我们在前三个示例中处理文本,而在最后一个示例中处理数字.
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.
要使其正常工作,您应该将所有内容都转换为 int
或 float
.这些 types
是数字,您可以不用担心就可以对它们进行数学运算.
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.
这篇关于final = P *((((1 +(r/n))**(n * t))))TypeError:/不支持的操作数类型:&#39; str&#39;和&#39; int&#39;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!