更新时间:2022-04-13 22:33:54
根据堆栈跟踪, 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
时,输入函数会将您输入的值存储为字符串(即文本).您希望它们成为数字.
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.