所以我有一个列表,playerlocation,我用它作为坐标。坐标是使用input()命令输入的,因此它们显示为字符串而不是整数。我以为这段代码可以解决这个问题,
playerlocation =(input("Player location?"))
playerlocation = list(playerlocation.split(","))
for x in playerlocation:
try:
x = int(x)
coordsareintegers = True
print(playerlocation)
except:
coordsareintegers = False但是print(playerlocation)返回了类似于'1',‘1’的东西,这意味着它们仍然是字符串。
我尝试过在使用坐标的地方使用int()命令,但这真的很繁琐。
发布于 2019-05-28 12:28:25
你可以在你的输入上使用map。类似于:
>>> coords = '1,2'
>>> split = coords.split(',')
>>> split
['1', '2']
>>> ints = map(int, split)
>>> ints
<map object at 0x0000000002476978>
>>> list(ints)
[1, 2]相应地应用于您的代码。
https://stackoverflow.com/questions/56334466
复制相似问题