无法将字符串转换为int

问题描述 投票:0回答:2

我目前从 python 2 开始使用 python 3。在 Python 2 中,可以使用

int()
函数轻松完成字符串类型转换,但我在 Py3.5.3 中遇到问题

current = input("Enter the Current price: ")
int(current)
print (type(current))
c = current - 24

即使在类型转换功能之后,当前仍显示为

Class str

错误是

TypeError: unsupported operand type(s) for -: 'str' and 'int'

我知道这只是一个小错误,但我在网上查找的示例都像我一样使用 int 函数。这里可能有什么问题

python python-3.5
2个回答
5
投票

您没有保存

int(current)
的返回值,它是整数表示形式。尝试一下

current = int(current)

应该可以了。


0
投票

您需要保存一个新变量。 而不是:

current = input("Enter the Current price: ")
int(current)
print (type(current))
c = current - 24

用途:

current = input("Enter the Current price: ")
intc = int(current)
print(type(intc))
c = intc - 24
© www.soinside.com 2019 - 2024. All rights reserved.