TypeError: '>' not supported between instances of 'method' and 'method'

TypeError: '>' not supported between instances of 'method' and 'method'

我打算尝试做一场海龟比赛,让你选择一个海龟的名字,并试着猜出哪只海龟会赢得比赛,到目前为止我遇到了一些问题,但现在我想不出来出了什么问题所以有人能告诉我为什么会出现这个错误代码吗? (我是堆栈溢出的新手)

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!")

@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()

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

变化:

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!")

编辑:

The xcor attribute of a Turtle object is not a class function but an instance method. – cdlane