Lab 1
Python a different way

Objectives

Preliminary Setup

  1. If you haven't already logged into a lab machine, do so now. I can help you with username/password information.
  2. 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 project folder:

  1. 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:
  1. 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 documentation official python documentation site.
  2. 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.
  3. 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 control-R. 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 like:
    XXX
    OOX
    OOX
    
  4. 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.
  1. 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.
  2. 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).
  3. Make it so that a message is printed if any of the rows is three-in-a-row (either X or O).
  4. [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 cassa@union.edu. Follow these instructions:

  1. In a terminal window, go to your workspace folder (the folder that contains the folders that are your PyCharm projects).
  2. Create the tarball with something like this:
    tar -cvzf CassLab1.tar.gz CassLab1
    See this page for an explanation of what that command is doing.
  3. Email the created file (the tar.gz tarball) to me.

Ask for help if you're having problems!