import turtle def move_forward(): turtle.forward(10) def move_left(): turtle.left(10) def move_right(): turtle.right(10) turtle.listen() turtle.onkeypress(move_forward, 'Up') turtle.onkeypress(move_left, 'Left') turtle.onkeypress(move_right, 'Right') turtle.done()
import turtle def change_color_red(): turtle.color('red') def change_color_green(): turtle.color('green') def change_color_blue(): turtle.color('blue') turtle.listen() turtle.onkeypress(change_color_red, 'r') turtle.onkeypress(change_color_green, 'g') turtle.onkeypress(change_color_blue, 'b') turtle.done()In the above example, the turtle's color changes to red, green, or blue when the respective keys are pressed. The package library used here is `turtle`.