Python Can';t将字符串转换为int
我目前从Python2开始使用Python3。在Python2中,可以使用Python Can';t将字符串转换为int,python,python-3.5,Python,Python 3.5,我目前从Python2开始使用Python3。在Python2中,可以使用int()函数轻松完成字符串类型转换,但我在Py3.5.3中遇到了问题 current = input("Enter the Current price: ") int(current) print (type(current)) c = current - 24 即使在typecast函数之后,current仍然显示为Class str 错误为TypeError:-:'str'和'int'的操作数类型不受支持。 我知道
int()
函数轻松完成字符串类型转换,但我在Py3.5.3中遇到了问题
current = input("Enter the Current price: ")
int(current)
print (type(current))
c = current - 24
即使在typecast函数之后,current仍然显示为Class str
错误为TypeError:-:'str'和'int'的操作数类型不受支持。
我知道这只是一个小错误,但我在网上查到的例子都使用了我使用的int函数。这里可能有什么问题您没有保存来自
int(current)
的返回值,它是整数表示形式。试一试
current = int(current)
这样就可以了。这是不正确的
int
在Python 2和3之间没有改变,改变的是input()
。Python 2的input()
等价物是raw\u input()
。在Python2中,input()
对输入求值,因此return
s一个int
我明白了,谢谢!我将删除答案中的最后一部分。