类型错误:不能用浮点类型的非整数乘以序列 [英] TypeError:can't multiply sequence by non-int of type float

查看:44
本文介绍了类型错误:不能用浮点类型的非整数乘以序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将 floatint 放在我的编码中,但它仍然说不能将序列乘以 float"

PV = input("投资金额:")r = float(input("rate:"))n = int(input("年:"))FV_conti = PV*(1+r)**n导入数学FV_diceret = PV * math.exp(r*n)

解决方案

问题在于 PV 是字符串而不是浮点数.input()Python3 没有与 Python2 不同,评估输入.

你需要把它转换成int/float:

PV = int(input("投资金额:"))

如果将一个字符串与一个 int 相乘,它将执行连接.这就是为什么乘以浮点数没有意义.

<预><代码>>>>PV = "123">>>光伏*2'123123'>>>光伏*2.3回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:不能将序列乘以float"类型的非整数

I tried to put the float and int inside my coding, but it still said "can't multiply sequence by non-int of type 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)

解决方案

The issue is that PV is a string and not a float. input() is Python3 does not evaluate the input unlike in Python2.

You need to convert it into a int/float:

PV = int(input("investment amout:"))

If you multiply a string with an int, it performs concatenation. That is why multiplying by float does not make sense.

>>> 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'

这篇关于类型错误:不能用浮点类型的非整数乘以序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆