STEP 10: We've chosen a random color, but now we need to actually set the pen to the new color.

  • Click the GRAPHICS tab in the toolkit, then click .
  • Drag out Set Color and drop it indented just under ····my_var·=·random.choice(my_list)¬
  • Change the parameter in .set_color() from "green" to the variable my_var.

Run your code and press the spacebar. Watch the pen randomly switch colors!

To navigate the page using the TAB key, first press ESC to exit the code editor.

stage.set_background_color("black") # sprite = codesters.Point(x, y) sprite = codesters.Point(50, 100) sprite.set_color("green") sprite.pen_down() def right_key(): sprite.move_right(20) # add other actions... stage.event_key("right", right_key) def left_key(): sprite.move_left(20) # add other actions... stage.event_key("left", left_key) def up_key(): sprite.move_up(20) # add other actions... stage.event_key("up", up_key) def down_key(): sprite.move_down(20) # add other actions... stage.event_key("down", down_key) my_list = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"] def space_bar(): my_var = random.choice(my_list) # add other actions... stage.event_key("space", space_bar)
  • Run Code
  • Submit Work
  • Next Activity
  • Show Console
  • Reset Code Editor
  • Codesters How To (opens in a new tab)