【问题标题】:TypeError: '>' not supported between instances of 'method' and 'method'TypeError:“方法”和“方法”的实例之间不支持“>”
【发布时间】:2020-12-14 17:14:13
【问题描述】:

我打算尝试进行一场乌龟比赛,让你选择一个乌龟的名字,并尝试能够猜出将赢得比赛的乌龟到目前为止我遇到了一些问题,但现在我无法弄清楚是什么出错了,有人能告诉我为什么会出现这个错误代码吗? (我是堆栈溢出的新手)

import turtle
import random
from turtle import *
from random import randint
speed()
penup()
goto(-140, 140)

for i in range(15):
  write(i,align = "center")
  right(90)

  for num in range(8):
    penup()
    forward(10)
    pendown()
    forward(10)

  penup()
  backward(160)
  left(90)
  forward(20)
A = turtle.Turtle()
A.shape('turtle')
A.color('red')
A.penup()
A.goto(-160, 100)

B = turtle.Turtle()
B.shape('turtle')
B.color('red')
B.penup()
B.goto(-160, 70)

C = turtle.Turtle()
C.shape('turtle')
C.color('red')
C.penup()
C.goto(-160, 40)
random.randint(1, 5)
random = random.randint(1, 7)
  
D = turtle.Turtle()
D.shape('turtle')
D.color('red')
D.penup()
D.goto(-160, 10)
for i in range(100):
  A.forward(randint(1,5))
  B.forward(randint(1,5))
  C.forward(randint(1,5))
  D.forward(randint(1,5))
if A.xcor > B.xcor and A.xcor > C.xcor and A.xcor > D.xcor:
  print("Wow the turtle you picked you picked wow the race!")

【问题讨论】:

    标签: python typeerror turtle-graphics python-turtle


    【解决方案1】:

    turtle.Turtle 对象的xcor 属性不是类变量, 而是一个类函数,因此您需要使用() 调用它来获取返回值。

    变化:

    if A.xcor > B.xcor and A.xcor > C.xcor and A.xcor > D.xcor():
      print("Wow the turtle you picked you picked wow the race!")
    

    到:

    if A.xcor() > B.xcor() and A.xcor() > C.xcor() and A.xcor() > D.xcor():
      print("Wow the turtle you picked you picked wow the race!")
    

    我更愿意为此使用内置的all() 函数:

    if all(A.xcor() > t.xcor() for t in [B, C, D]):
      print("Wow the turtle you picked you picked wow the race!")
    

    编辑:

    Turtle 对象的xcor 属性不是类函数,而是实例方法。 --cdlane

    【讨论】:

    • Turtle 对象的xcor 属性不是class 函数,而是instance 方法。
    • @cdlane 那么有没有类函数之类的东西呢?
    • @AnnZen 这将被称为classmethod
    【解决方案2】:

    @RandomDavis 在他们的评论 (+1) 中涵盖了您的主要错误,但还有其他问题。例如。 speed()random.randint(1, 5) 本身是无操作的。此外,当您必须以两种不同的方式同时导入 turtle 和 random 时,您会遇到麻烦。让我们将您的代码重新编写成一个有效的程序:

    from turtle import Screen, Turtle
    from random import randint
    
    screen = Screen()
    
    marker = Turtle()
    marker.speed('fastest')
    marker.penup()
    marker.goto(-140, 140)
    
    for number in range(15):
        marker.write(number, align="center")
        marker.right(90)
    
        for _ in range(8):
            marker.penup()
            marker.forward(10)
            marker.pendown()
            marker.forward(10)
    
        marker.penup()
        marker.backward(160)
        marker.left(90)
        marker.forward(20)
    
    marker.hideturtle()
    
    A = Turtle()
    A.shape('turtle')
    A.color('red')
    A.penup()
    A.goto(-160, 100)
    
    B = Turtle()
    B.shape('turtle')
    B.color('red')
    B.penup()
    B.goto(-160, 70)
    
    C = Turtle()
    C.shape('turtle')
    C.color('red')
    C.penup()
    C.goto(-160, 40)
    
    D = Turtle()
    D.shape('turtle')
    D.color('red')
    D.penup()
    D.goto(-160, 10)
    
    for _ in range(100):
        A.forward(randint(1, 5))
        B.forward(randint(1, 5))
        C.forward(randint(1, 5))
        D.forward(randint(1, 5))
    
    if A.xcor() > B.xcor() and A.xcor() > C.xcor() and A.xcor() > D.xcor():
        print("The turtle you picked you picked won the race!")
    
    screen.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-07
      • 2021-11-21
      • 1970-01-01
      • 2017-09-14
      • 2017-09-28
      • 2018-01-10
      相关资源
      最近更新 更多