Ich habe ein Programm geschrieben, das einen zufälligen Koordinaten-/Farbsatz aus einer Liste zieht und einen Kreis in ein Grafikfenster zeichnet. Ich will es stoppen Zeichnen von Kreisen, sobald jede Koordinate/Farbpalette mindestens einmal gezeichnet wurde. Ich möchte auch dokumentieren, wie oft jeder der 9 möglichen Koordinaten/Farbsätze verwendet wurde. Ich habe einen Zähler initialisiert, count = [0, 0, 0, 0, 0, 0, 0, 0, 0]
, den ich jedesmal drucken möchte, wenn ein neuer Kreis gezeichnet wird, mit der Anzahl der Male, die jeder Status verwendet worden ist. Tipps, wie das geht?Counter-Liste in Python
Beispiel Ausgabe in der Schale:
count = [1, 0, 0, 0, 0, 0, 0, 0, 0]
count = [2, 0, 0, 0, 0, 0, 0, 0, 0]
count = [2, 1, 0, 0, 0, 0, 0, 0, 0]
count = [2, 1, 0, 0, 1, 0, 0, 0, 0]
count = [2, 1, 1, 0, 1, 0, 0, 0, 0]
count = [2, 1, 1, 0, 1, 0, 0, 0, 1]
count = [2, 1, 1, 0, 1, 0, 1, 0, 1]
count = [2, 1, 1, 0, 1, 0, 1, 1, 1]
count = [2, 1, 1, 1, 1, 0, 1, 1, 1]
count = [2, 1, 1, 1, 1, 1, 1, 1, 1]
Bitte und Danke im Voraus!
import random, turtle
wn = turtle.Screen()
alex = turtle.Turtle()
alex.pensize(3)
alex.color("black")
def turtle_draw():
'''draws a circle with given coordinates and in a
given color, prints a counter.'''
color, place = random_state_finder()
alex.pu()
alex.goto(place)
alex.shape("circle")
alex.shapesize(3)
alex.fillcolor(color)
count = [0, 0, 0, 0, 0, 0, 0, 0, 0]
wn.ontimer(turtle_draw, 250)
def random_state_finder():
'''randomly generates a state number from 0 to 8
and assigns the state's data to color and place.'''
rng = random.Random()
state_num = rng.randrange(0, 8)
L = [((-150, 150), "red"), ((0, 150), "orange"),
((150, 150), "yellow"), ((-150, 0), "green"),
((0, 0), "blue"), ((150, 0), "violet"),
((-150, -150), "cyan"), ((0, -150), "magenta"),
((-150, -150), "purple")]
random_state = L[state_num]
color = random_state[1]
place = random_state[0]
return color, place
turtle_draw()
Für den Anfang, Sie müssen jedes Mal, wenn ein Zustand ausgewählt wird, "count" aktualisieren; um das zu tun, kann es nicht nur lokal für 'turtle_draw' definiert werden. –