Lesson goal: Getting input from the mouse

Previous: Reading text from the keyboard | Home | Next: Square roots and the distance formula

In a previous lesson, you learned how to use the input() function to read text from the keyboard. What about the mouse, which is another extremely important way of interacting with the computer?

There's another function dedicated to reading the mouse call mouse(). Unlike the input() function which returns the text typed at the keyboard, the mouse() function returns two values, the $x$ and $y$ coordinates of where the mouse was clicked. Here's how the function works:

x,y=mouse()
Move the mouse over a dotted box for more information.

Now you try. Run this code several times, clicking on different regions of the output window. Does the mouse() function seem to be reading your click position?

Type your code here:


See your results here:

Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.