Python a different way
- Get acquainted with the PyCharm development environment
- To re-acquaint yourself with programming in Python
- If you haven't already logged into a lab machine, do so now. I can
help you with username/password information.
- Make a "csc120" folder In that
folder, make a "workspace" folder. You can use this
folder for labs and projects. Each lab or project will be a
folder within the workshop folder.
Integrated Development Environments
In previous classes where you've used Python, you used a program called
Idle to edit and run your programs. However, Python does not
equal Idle. Python is a language in which you can
write programs, but the tool you use to edit those programs is
simply a text editor. Any text editor will do. [Note: Similarly,
English is not Microsoft Word].
Once your programs are in the right language (in this case Python), they
can be compiled or interpreted by appropriate tools (in
this case, the Python interpreter). When you used Idle before, it
provided a window for text file editing (where you could type your
programs) and a way to run those programs. To run the programs, Idle would
call the Python interpreter to do the job. Because editing and running
are integrated together in Idle, we call Idle an Integrated
Development Environment (IDE).
Another IDE, one used by professional developers, is
called PyCharm. PyCharm is made by the JetBrains company,
which also makes other IDEs, including ones designed for Java, C++,
Ruby, etc.... In this course, you will use PyCharm instead of Idle.
But remember, the language you are using is still the same old
Python you are familiar with.
Getting Started with PyCharm
Launch PyCharm from the dock icon, if one is available, or you can
find it by using the launcher. You'll be presented with options
to Create New Project or Open an existing one. Choose the Create New Project option, which will result in a windo like the following:
You can click the button with the 3 dots to the right of the
Location field to navigate to the csc120/workspace folder you created
earlier. In the example above, I'm creating a project called CassLab1
in my workspace folder. Please name your project with your
name and lab number as well.
Add some Starter Code
In many labs, including this one, I'll be providing you with
starter code that you need to augment with your own code. Download
and add tictactoc.py to your
- Right-click the link above and choose Save as...,
and save the file to your project folder. Then in the lefthand pane in
PyCharm, you should see that tictactoe.py is in the project.
Have a look around
Now that you have a project and some code, explore to see what you have:
- Read the code. Can you figure out what each line is doing? If there
are functions you don't know, look up the at the
python documentation site.
- Look at your files and folders on the flashdrive. Notice that
your project is just a folder (named the same as your project) that
contains the .py files you've added to it.
- Run the code. The first time you run the code, you need to tell
PyCharm what you want to run: choose "Run..." from the "Run" menu,
and select tictactoe.py. Afterwards, you can re-run by
clicking the green play button in the toolbar, or by pressing
Notice that when the program runs, it will probably complain that
tictactoe_board.txt doesn't exist; so ask PyCharm to make a new
file called tictactoe_board.txt. Fill it will some text
- Now that you've run the code successfully, take a look at the
output. Based on what you see in the output, what kind of variable is
'rows' in the code?
Add some more code
For the following, make each of these additions separately, one at a time.
After each one, test that your program works (this might involve making new
board files to test with). Do not try to jump in and make all of these
changes at once.
- Add to or modify the code I've given you so that it prints an error
message if the number of rows is not exactly 3.
- Make it so that an error message is printed if the number of columns
is not exactly 3. (i.e. if any row has fewer or more than 3 characters).
- Make it so that a message is printed if any of the rows is
three-in-a-row (either X or O).
- [Extra Credit] Make it so that a message is printed if there is three
in a row, horizontally, vertically, or diagonally.
How to turn in this lab
Before turning in any program in this class, remember this mantra:
Just because it works doesn't mean it's good.
Part of your grade will also come from things like how understandable
and readable your code is. You will also be graded on the neatness,
presentation, and style of your program code.
For all labs, turn in only an electronic version. Please create a
tarball of your PyCharm project and email it to me at email@example.com.
Follow these instructions:
- In a terminal window, go to your workspace folder (the folder that
contains the folders that are your PyCharm projects).
- Create the tarball with something like this:
tar -cvzf CassLab1.tar.gz CassLab1
page for an explanation of what that command is doing.
- Email the created file (the tar.gz tarball) to me.
Ask for help if you're having problems!