Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用;属性错误:';海龟';对象没有属性';色彩模式'&引用;尽管Turtle.py具有颜色模式属性_Python_Error Handling_Attributes - Fatal编程技术网

Python &引用;属性错误:';海龟';对象没有属性';色彩模式'&引用;尽管Turtle.py具有颜色模式属性

Python &引用;属性错误:';海龟';对象没有属性';色彩模式'&引用;尽管Turtle.py具有颜色模式属性,python,error-handling,attributes,Python,Error Handling,Attributes,我试着运行使用Turtle库的代码,如图所示 import turtle import random def main(): tList = [] head = 0 numTurtles = 10 wn = turtle.Screen() wn.setup(500,500) for i in range(numTurtles): nt = turtle.Turtle() # Make a new turtle, initial

我试着运行使用Turtle库的代码,如图所示

import turtle
import random

def main():
    tList = []
    head = 0
    numTurtles = 10
    wn = turtle.Screen()
    wn.setup(500,500)
    for i in range(numTurtles):
        nt = turtle.Turtle()   # Make a new turtle, initialize values
        nt.setheading(head)
        nt.pensize(2)
        nt.color(random.randrange(256),random.randrange(256),random.randrange(256))
        nt.speed(10)
        wn.tracer(30,0)
        tList.append(nt)       # Add the new turtle to the list
        head = head + 360/numTurtles

    for i in range(100):
        moveTurtles(tList,15,i)

    w = tList[0]
    w.up()
    w.goto(0,40)
    w.write("How to Think Like a ",True,"center","40pt Bold")
    w.goto(0,-35)
    w.write("Computer Scientist",True,"center","40pt Bold")

def moveTurtles(turtleList,dist,angle):
    for turtle in turtleList:   # Make every turtle on the list do the same actions.
        turtle.forward(dist)
        turtle.right(angle)

main()
在我自己的Python编辑器中,我遇到了以下错误:

海龟。海龟图案错误:错误的颜色顺序:(236197141)

然后,基于上的回答,我在“nt.color(……”)之前添加了这一行

nt.colormode(255)

现在它向我展示了这个错误

AttributeError:“Turtle”对象没有属性“colormode”


好的,所以我检查了我的Python库并查看了Turtle.py的内容。colormode()属性肯定存在。是什么使代码能够在原始站点上运行,而不能在我自己的计算机上运行?

问题是您的
Turtle
对象(
nt
)没有
colormode
方法。不过,海龟模块本身就有一个

所以你只需要:

turtle.colormode(255) 
而不是

nt.colormode(255)
编辑:为了澄清您在评论中的问题,假设我创建了一个名为
test.py
的模块,其中包含一个函数和一个类“test”:

# module test.py

def colormode():
    print("called colormode() function in module test")

class Test
    def __init__(self):
        pass
现在,我使用这个模块:

import test

nt = test.Test()  # created an instance of this class (like `turtle.Turtle()`)
# nt.colormode()  # won't work, since `colormode` isn't a method in the `Test` class
test.colormode()  # works, since `colormode` is defined directly in the `test` module

你能发布或链接到你正在使用的代码吗?我有一个有根据的猜测,出了什么问题,但由于你发布的信息有限,有点难以确定。啊,我没有注意到代码最初是隐藏的——我注意到的只是很多其他链接。这确实有效,谢谢。它现在确实编译了这部分代码,但我只是不太明白,如果其他“nt”行都有代码事先没有定义的方法,为什么我的原始行不起作用。因此colormode()函数超出了它的范围。这完全有道理,谢谢!