2016-12-04 2 views
0

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() 
+0

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. –

Antwort

0

In gewisser Weise ist es einfacher als Sie es machen. Der Schlüssel ist random.shuffle() - mischen Sie einfach die Staaten Liste einmal dann gehen Sie durch die gemischte Liste eins nach dem anderen. Die Komplikationsrate ist Ihre count Liste, die bedeutet, dass wir tatsächlich einen Index-Liste in die Staaten Liste machen und die Indexliste mischen, die ursprünglichen Zustände unangetastet bleiben Liste:

from turtle import Turtle, Screen 
from random import shuffle 

states = [((-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")] 

def turtle_draw(): 
    ''' 
    draws a circle with given coordinates 
    and in a given color, prints a counter. 
    ''' 

    index = indexes.pop() 

    place, color = states[index] 
    alex.goto(place) 
    alex.fillcolor(color) 

    counts[index] += 1 
    print(counts) 

    if indexes: 
     screen.ontimer(turtle_draw, 250) 

counts = [0 for state in states] 

indexes = [i for i, state in enumerate(states)] # could also use list(range(len(states))) 

shuffle(indexes) 

screen = Screen() 

alex = Turtle(shape="circle", visible=False) 
alex.shapesize(3) 
alex.penup() 
alex.color("white") # don't want to see alex until first state 
alex.showturtle() 

turtle_draw() 

screen.exitonclick() 

TERMINAL OUTPUT

> python3 test.py 
[0, 0, 1, 0, 0, 0, 0, 0, 0] 
[0, 0, 1, 1, 0, 0, 0, 0, 0] 
[0, 0, 1, 1, 0, 0, 0, 1, 0] 
[1, 0, 1, 1, 0, 0, 0, 1, 0] 
[1, 0, 1, 1, 0, 0, 0, 1, 1] 
[1, 0, 1, 1, 0, 1, 0, 1, 1] 
[1, 0, 1, 1, 0, 1, 1, 1, 1] 
[1, 1, 1, 1, 0, 1, 1, 1, 1] 
[1, 1, 1, 1, 1, 1, 1, 1, 1] 
>