我想将海龟“person”添加到“infected_people”列表中,但出现错误“TurtleGraphicsError:bad color string:red”
如何在不出错的情况下将“个人”添加到“感染者”列表中
我是编程新手,如果我的解释不清楚,我很抱歉
infected_people = []
i = 0
while i < 30: #number of steps (time)
for person in people:
random_walk(30, 400)
i + 1
for infected_person in infected_people:
if person.distance(infected_person) < 30:
person.color("red")
infected_people.append(person)
下面是我代码的另一部分:
import turtle
import random
wn = turtle.Screen()
def person_characteristics():
for person in people:
person.penup()
person.shape("circle")
person.shapesize(0.2)
x = random.randint(-200, 200)
y = random.randint(-200, 200)
person.setpos(x, y)
person.speed(0)
turtle.update()
return person
def population(population_size):
turtle.update()
people = []
for _ in range(population_size):
people.append(turtle.Turtle())
return people
def person_characteristics():
for person in people:
turtle.update()
person.penup()
person.shape("circle")
person.shapesize(0.2)
person.speed(0)
x = random.randint(-200, 200)
y = random.randint(-200, 200)
person.setpos(x, y)
return person
def random_walk(step_size, area_size):
person.clear()
count = 0
while count < 1:
count += 1
if (-area_size < person.xcor() <area_size) and (-area_size < person.ycor() <area_size):
person.right(random.randint(0,360))
person.forward(step_size)
else:
person.right(180)
person.forward(step_size)
turtle.update()
people = population(50)
person = person_characteristics()
def infect_random(people):
infected = random.choice(people)
infected.color("red")
return infected
infected_people = []
initial_infected = infect_random(people)
infected_people.append(initial_infected)
print(infected_people)
counted_infections = 0
我的目标是让每一个被感染的人(红点)感染到其他每一个接近他的人。现在只有最初被感染的人会感染其他人。所以我想如果我把每一个被感染的人都加到被感染的人的名单上,那么它就会起作用。但是,当添加行infected\u people.append(person)时,我得到一个错误
i = 0
while i < 30: #number of steps (time)
for person in people:
random_walk(30, 400)
i += 1
for infected_person in infected_people:
if person.distance(infected_person) < 30:
person.color("red")
infected_people.append(person)
turtle.done()
wn.exitonclick()
这就是我得到的错误:
runfile('C:/Users/Noa Hoogeweg/Documents/BMT/PvL/Virus/Noa_Virus_goed.py', wdir='C:/Users/Noa Hoogeweg/Documents/BMT/PvL/Virus')
[<turtle.Turtle object at 0x000001FF4D3D9908>]
Traceback (most recent call last):
File "C:\Users\Noa Hoogeweg\Documents\BMT\PvL\Virus\Noa_Virus_goed.py", line 78, in <module>
person.color("red")
File "C:\Users\Noa Hoogeweg\anaconda3\lib\turtle.py", line 2216, in color
pcolor = self._colorstr(pcolor)
File "C:\Users\Noa Hoogeweg\anaconda3\lib\turtle.py", line 2696, in _colorstr
return self.screen._colorstr(args)
File "C:\Users\Noa Hoogeweg\anaconda3\lib\turtle.py", line 1158, in _colorstr
raise TurtleGraphicsError("bad color string: %s" % str(color))
TurtleGraphicsError: bad color string: red
您对
person.color("red")
的调用看起来非常有效,在运行代码时我没有发现错误从错误的stacktrace开始,海龟对颜色字符串只做两件事。首先,它测试是否是
str
您必须通过该测试才能获得错误。其次,它将颜色字符串传递给tkinter的winfo_rgb()
方法,该方法返回RGB三元组。Turtle忽略了三元组,它只是想看看该函数是否成功或抛出错误。如果它抛出错误,您将收到显示的消息:因此,球在特金特的球场上,尝试一下下面的小程序,看看特金特是否为你做了正确的事情:
如果它起作用,您应该得到如下结果:
在这种情况下,您的turtle代码与tkinter库的连接方式存在可疑之处。如果上述操作失败,您应该得到如下结果:
在这种情况下,您可能需要检查tkinter安装的有效性
即使修复了
"red"
问题,代码中也存在其他错误,这些错误将使其无法工作。下面是我为使其正常运行而进行的重写:我不知道您得到了什么错误,但是
i + 1
行不会增加I,要增加I的值,您需要使用i = i + 1
或i += 1
相关问题 更多 >
编程相关推荐