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),搜索过每一个地方并且仍然坚持这一点。
我明白我有一个字符串,但需要把它变成一个整数作为最终答案。下面是我输入的代码来测试它。我坚持我得到的错误。
这是一个复利计算器,用户必须输入本金,年利率,复合频率和年份。
我做了一些研究,并注意到我可以使用('inf')p,n和r作为声明说可以是大数字也可以是小数字。基本上用户可以输入任何数字。
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
对不起等待。好吧,我相信我能通过@Rafael Cardoso推荐的Stop and Think让它正常工作。谢谢你。
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
是有道理的。区别在于我们正在处理前3个示例中的文本,并且在最后一个示例中使用数字。
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
。这些类型
是数字,您可以毫不费力地使用它们进行数学运算。
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:/:'str'和'int'的不支持的操作数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!