Once you have downloaded a copy of PyCharm as described in the “Installing PyCharm” handout on the course website, your next task is to understand how to write and run Karel programs using PyCharm.
Day1PM.zip
. Unzip it by double-clicking.Navigate to the Day1PM folder and open it. Open the folder, rather than a particular file you want to edit.
If you already have another project open, PyCharm will prompt you to either open the project in your existing window or in a new window. You can select either.
You should see a view shown in Figure 1 below, right-hand side.
Figure 1: The first window that opens when you start PyCharm (left) should have an Open option for you to select a project. After opening a project, you should see the default PyCharm landing view (right).
The Project toolbar on the left (Figure 1, right) shows the current project we are working on (Day1PM) as well as all of its contents: folders, Python files, etc. For Karel programs, you do not need to inspect or edit any files in the karel or worlds folders.
Double-click on a program (say, newspaper_karel.py
) and it will open up a text editor view in the main window of PyCharm.
It's time to code!
Figure 2: The PyCharm terminal used to run Python programs.
To run a program in PyCharm, click the "Terminal" option at the bottom of the screen. To run any program, all you have to do is type the following command into the Terminal and hit "Enter":
python3 insert_name_here.py
py insert_name_here.py
The terminal (also known as a console) will then print output and accept user text input, depending on how you write your program.
As a side note, in many handouts we might use the Mac convention of running programs by using the command python3 <program name>
. If you are a PC user, you should instead use the command py <program name>
. Just by convention on the PC, you run the Python interpreter using the command py
(as opposed to python3
on the Mac).
Karel programs are also Python programs!
Figure 3: The Karel display that appears when running `newspaper_karel.py`, including Karel’s world, a Run Program button, and a Load World button
If you then press the Run Program button, Karel will go through the steps in the `main() function that you wrote.
Use the scroll bar to speed Karel up.
Exit out of the window when you are done. The next time you run any Karel program from the PyCharm Terminal (even if it's the same program), it will create a new Karel window.
Suppose we are working on Hospital Karel and we think we have gotten our code to work in the default world. Press the Load World button and select and open the world in which you want to run Karel. You can then test Karel in this new world by clicking on the Run Program button.
There are several options when opening a project. * This Window: Replaces the current project in your current window with the new project, closing the current project. * New Window: Opens the new project in a new, separate window, keeping your current project in the original window. * Attach: Adds the new project folder to your current window, keeping the old and new projects separate but both open. * Cancel: Don't open the new project.