The following exercises will help you get started with GitHub Copilot. You must have completed the setup instructions before starting these steps.
1a. Building a Rock Paper Scissors game with GitHub Copilot
- Ensure you can see the files in the Explorer view. If not, click the Explorer View icon on the left sidebar of your editor.
-
Open the
main.py
file if it's not already open in the editor. -
Let's start by adding the following comment to provide some context for the code we're about to write:
# Write a rock, paper, scissors game
↩️ -
On the next line we're going to prompt GitHub Copilot to suggest code for us by typing the following:
# import random module
↩️
- When you press Enter after typing the previous comment, GitHub Copilot will suggest some code for you. Select the first suggestion by pressing TAB then Enter again.
- Now we're going to prompt GitHub Copilot to suggest code for us by typing the following:
# define main function that handles all the logic
↩️
- When you press Enter after typing the previous comment, GitHub Copilot will again suggest some code for you. Select the first suggestion by pressing TAB then Enter again.
- Pause briefly while Copilot creates up to 10 suggestions for you. You should see the Copilot icon in the lower right corner of the editor spinning. When Copilot displays the first suggestion, we're going to open the GitHub Copilot suggestion panel by pressing CTRL + ENTER.
- Scroll down the list of suggestions that GitHub Copilot has made and choose the one that looks like the best option for our Rock, Paper, Scissors game. When you see the suggestion you want, click Accept Solution to have that code snippet inserted into your code file.
- On the line following the last snippet, let's prompt GitHub Copilot to suggest the final line of code for us by typing the following:
# call main function
↩️
- When you press Enter after typing the previous comment, GitHub Copilot will suggest some code for you. Select the first suggestion by pressing TAB then Enter again.
Now we're ready to see if this code executes 👍
NOTE: To run the Python code, you'll need to have Python installed on your computer.
- In the Terminal window in your Codespace, type the following command and press Enter to run the code:
python3 main.py
↩️
Here's an example of what the completed game might look like.
Hopefully your Paper, Rock, Scissors game is working! Remember, GitHub Copilot is probabilistic so you may not get the exact same code suggestions as we did. If you're not happy with the suggestions, you can always press CTRL + Z to undo the changes and try again.
You're now ready to start the challenge exercises to see how you can leverage the power of GitHub Copilot to solve a number of challenges yourself.
======================== END OF EXERCISE ========================
-- or --
IF you chose to use VSCode as your IDE AND you chose to install the CodeTour extension, OR you're using the Codespaces experience and want to use the CodeTour extension instead of reading through the instructions, you should use the following steps instead.
1b. Building a Rock Paper Scissors game with GitHub Copilot (CodeTour option)
- Ensure you can see the files in the Explorer view. If not, click the Explorer View icon on the left sidebar of your editor.
- At the bottom of the Explorer view panel, click CodeTour to expand the CodeTour panel.
- In the CodeTour panel, press the “Play button” to start the tour.
- Your CodeTour will begin! Follow the CodeTour’s steps in the main code window to learn how to use GitHub Copilot.
- When you've completed each step, click the Next button to move to the next step in the CodeTour.
- Work your way through each of the steps in the CodeTour to complete this exercise.
You're now ready to start the challenge exercises to see how you can leverage the power of GitHub Copilot to solve a number of challenges yourself.