2024-06-28 19:35:39 发布
网友
我试着把float和int放在我的代码中,但它仍然说“不能用float类型的非int乘以序列”
float
int
PV = input("investment amout:") r = float(input("rate:")) n = int(input("year:")) FV_conti = PV*(1+r)**n import math FV_diceret = PV * math.exp(r*n)
问题是PV是一个字符串而不是一个float。input()is Python3不像Python2那样计算输入。你知道吗
input()
您需要将其转换为int/float:
PV = int(input("investment amout:"))
如果将字符串与int相乘,它将执行串联。这就是为什么乘浮点数是没有意义的。你知道吗
>>> PV = "123" >>> PV*2 '123123' >>> PV*2.3 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can't multiply sequence by non-int of type 'float'
问题是PV是一个字符串而不是一个float。
input()
is Python3不像Python2那样计算输入。你知道吗您需要将其转换为
int
/float
:如果将字符串与int相乘,它将执行串联。这就是为什么乘浮点数是没有意义的。你知道吗
相关问题 更多 >
编程相关推荐