1

こんにちは、

カメを使ってウイルスの発生をシミュレートしています。私は次のコードを思いつきました、私の質問はコードの後に​​あります:

import turtle
import random
import time

def make_population(amount):
    """
    Creates a list representing a population with a certain amount of people.
    """
    population = []
    for person in range(amount):
        population.append(turtle.Turtle())
    for person in population:
        person.shape("circle")
        person.shapesize(0.2)
    return population

def random_move(person):
    """
    Makes a turtle move forward a random amount and then turn a random amount.
    """
    person.forward(random.randint(0,20))
    person.right(random.randint(-180,180))

def check_boundary(person):
    """
    Checks if a turtle is still within the given boundaries.
    """
    if -250 <= person.xcor() <= 250 and -250 <= person.ycor() <= 250:
        return
    person.setpos(random.randint(-200,200),random.randint(-200,200))

def infect_random(population):
    """
    Gets a random item from the population list and turns one red
    """
    infected = random.choice(population)
    infected.color("red")
    return infected

def infect_person(person):
    """
    Makes the turtle infected
    """
    infected_person = person.color("red")
    return infected_person

def simulation(amount, moves = 0):
    """
    Simulates a virus outbreak
    """
    border = 500
    window = turtle.Screen()
    turtle.setup(500,500)
    turtle.tracer(0)
    population = make_population(amount)
    for person in population:
        person.penup()
        person.setpos(random.randint(-250,250),random.randint(-250,250))
    turtle.update()
    infected = infect_random(population)
    for move in range(moves):
        turtle.tracer(0)
        for person in population:
            random_move(person)
            if person.distance(infected) < 50:
                infect_person(person)
            check_boundary(person)
        turtle.update()
        time.sleep(0.5)

    window.exitonclick()

そのため、シミュレーションが開始されるとランダムに 1 人に感染し、他のカメが近づくと (たとえば 50 ピクセル以内)、それらも感染して赤くなります。ただし、これらの新しく「感染した」カメは、最初のカメと比較して「感染」していないため、他のカメに感染しません。感染者 = 感染者(人) に変更しようとしましたが、これはエラーになります。私はしばらく立ち往生していて、助けてくれる人がいるかどうか疑問に思っていました. 私はまた、2 つのリストを作成することも考えました: 人口と感染した人口はおそらく私の問題を解決できるかもしれませんが、コードの残りの部分でそれを実装する方法を理解できませんでした。

前もって感謝します

4

2 に答える 2