类型错误:can't将序列与类型的非整数相乘

2024-06-28 19:35:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着把floatint放在我的代码中,但它仍然说“不能用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)

Tags: 代码import类型inputrate序列mathfloat
1条回答
网友
1楼 · 发布于 2024-06-28 19:35:39

问题是PV是一个字符串而不是一个float。input()is Python3不像Python2那样计算输入。你知道吗

您需要将其转换为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'

相关问题 更多 >