【问题标题】:TypeError:can't multiply sequence by non-int of type floatTypeError:不能将序列乘以浮点类型的非整数
【发布时间】:2018-12-19 01:26:55
【问题描述】:

我尝试将floatint 放入我的编码中,但它仍然说“不能将序列乘以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)

【问题讨论】:

  • 这是什么语言?如果是python,那就试试PV = int(input("investment amout:"))
  • 是python。谢谢!我弄明白了。
  • 你弄明白了吗?或者@RohithS98 的评论对你有什么帮助?在后一种情况下,请你们两个回答并接受。

标签: python typeerror


【解决方案1】:

问题在于 PV 是字符串而不是浮点数。 input()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'

【讨论】:

    猜你喜欢
    • 2013-09-11
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 2011-04-06
    • 2012-09-17
    相关资源
    最近更新 更多