类型错误:无法将序列乘以 float 类型的非 int

2024-02-08

我试图把float and int在我的编码中,但它仍然说“不能将序列乘以非 int 类型float"

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 是字符串而不是浮点数。input() is Python3 https://docs.python.org/3/library/functions.html#input不像中那样评估输入Python2 https://docs.python.org/2/library/functions.html#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'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

类型错误:无法将序列乘以 float 类型的非 int 的相关文章

随机推荐