【问题标题】:How do I fix the TypeError 'builtin_function_or_method' object is not subscriptable [closed]如何修复 TypeError 'builtin_function_or_method' 对象不可下标 [关闭]
【发布时间】: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


【解决方案1】:

coords.split 是一种方法。您不能为方法下标(获取项目)。您想从coords.splitresult 中获取第一项。为此,您必须实际调用该方法。

turtle.forward(float(coords.split()[0])*20)

【讨论】:

    猜你喜欢
    • 2012-01-09
    • 1970-01-01
    • 2020-02-03
    • 2020-11-05
    • 1970-01-01
    • 2016-08-20
    • 2020-01-30
    • 1970-01-01
    相关资源
    最近更新 更多