【发布时间】:2022-01-18 21:31:18
【问题描述】:
我正在尝试使用 split() 将字符串拆分为两个单独的坐标。
标题中的TypeError 回调了这些行:
#Coordinate input
coords = input("Please enter the coordinates of the starting point separated by a space:")
#Turtle moves towards coordinate
turtle.penup()
turtle.forward(float(coords.split[0])*20)
错误是:
TypeError: 'builtin_function_or_method' object is not subscriptable
我试图阅读它,但据我所知,这些案例都与我正在尝试做的事情无关。
【问题讨论】:
-
coords.split[0]的意图是什么?你的意思是写coords.split()[0]? -
输入应该是一组两个数字,第一个是点的 x 坐标。 coords.split[0] 应该在这种情况下找到第一个单词/数字,然后让海龟沿着 x 轴向该点移动。我刚读了你评论的第二部分,不知道我是怎么错过的。这是解决方案,谢谢!
-
还要注意
input()返回一个字符串,所以在split()之后你需要将返回的两个值分别转换成整数。 -
我在这里:
float(coords.split[0])*20
标签: python split typeerror turtle-graphics python-turtle