Python 类型错误:Can';t转换为';int';对象隐式转换为字符串

Python 类型错误:Can';t转换为';int';对象隐式转换为字符串,python,int,Python,Int,我不知道我做错了什么任何帮助都会被感激的 打印(类型(浮动(金额+3))) TypeError:无法将“int”对象隐式转换为str 如果变量金额来自lstrip结果,则表示金额类型为str,则退出代码为1的过程结束。 因此,考虑到这一点,这句话 title = link.string amount = title.strip().lstrip("$") try: print(type(float(amount))) check = "True" print(amou

我不知道我做错了什么任何帮助都会被感激的

打印(类型(浮动(金额+3))) TypeError:无法将“int”对象隐式转换为str

如果变量金额来自lstrip结果,则表示金额类型为str,则退出代码为1的过程结束。 因此,考虑到这一点,这句话

title = link.string
amount = title.strip().lstrip("$")


try:
    print(type(float(amount)))
    check = "True"
    print(amount + 30) #This doesnt work
except ValueError:
    print(type(amount))
    check = "False"
将返回一个异常。 您应该在尝试添加数字之前分析金额(在这种情况下键入int):

Python 3中的例外是:

TypeError:无法将“int”对象隐式转换为str

但在Python 2中:

TypeError:无法连接'str'和'int'对象

你的判决:

float(amount) + 30
第一项决议:

print(amount + 30)
然后把结果打印出来。错误发生在打印前,添加时。
Python在这种情况下不会隐式解析文本。

您的错误与代码不匹配,不管我想象您希望
float(amount)+3
amount=float(title.strip().lstrip($)
@padraiccningham
amount=float(title.strip().lstrip($)
工作,但我不明白为什么。顺便谢谢你的帮助
print(amount + 30)
amount + 30