Due: Thursday, September 10th by Class Time
Welcome to CS 110! First, we'll get registered for Turingscraft. For the remainder of this lab, you'll learn to create and run a basic program in Python. You will also introduce a number of deliberate mistakes into your programs so as to familiarize yourself with some of Python's error messages. Last, you'll save your program to your personal folder (for yourself), and to your assignment inbox (for grading).
Here we'll help get you up and running on TuringsCraft. To start, go to http://www.turingscraft.com/ and click on the link to register for your CodeLab account and follow the instructions. The required section access codes are as follows:
Section 1: DENISO-7117-0 (for the 10:30 class)
Section 2: DENISO-8190-0 (for the 8:30 class)
Note that there are no letters after the first hyphen. When it asks for a valid email address, you must use your Denison email. Pick a password you'll remember.
After completing the remaining questions, you should get a message stating "You are registered for 1 course", with this course listed below. There are two buttons here, "load" and "get full access". You currently have a trial version that will allow you to complete 10 problems before you are required to pay. Before you can complete the first Turingscraft assignment (due Monday), you'll need to get full access. The cost is $25, and you can use a credit card, PayPal, or check or money order. Speak with me if you're having problems.
For now, it'll suffice to just click "load". This will bring you to the main interface for TuringsCraft. On the left you'll see a pane containing exercises sorted by topic. Click the + sign to open a top level topic, or click the arrow key to open a topic and all subfolders. Just to practice, let's open the first section, entitled 'Expressions and Variables', and then navigate into 'Numeric Expressions' to the first exercise, labeled '50029'. You can also navigate by repeatedly clicking the blue 'Next' button on the right side of the user interface.
At this point, you should see some 15 problems (represented as numbers) with an empty box next to each in the left pane. If you have not already done so, click on the first problem (50029). To the right of the topic/exercise pane, you'll see a set of instructions for the problem and an interaction window. Read the instructions carefully, and try entering your solution. Once entered, hit the submit button.
Once you've entered a correct solution, the white box in the left navigation pane should turn green. The color of the box allows you (and me) to tell which problems you've solved, which ones you've tried by got wrong, which were late, and which you haven't attempted. You can make as many attempts as you want without reprecussion, so long as you've submitted a correct answer by the due date for the problem. In fact, even after you've gotten the right answer, you can try modifying it, and it will still record that you've gotten the answer correctly. This might be handy if you'd like to see whether some part of your solution was actually necessary, or you just want to try an alternate approach.
Your first TuringsCraft assignment is to complete the designated exercises by next Monday (9/7/08). This means you'll need to get the full version of TuringsCraft before then. Again, come see me if that is causing problems for you. Also note that you can navigate within the left pane with exercises sorted by deadline instead of by topic. Simply select 'by deadline' from the drop down list located just above the navigation pane.
As you have by now read, there are multiple phases involved in the software development process. Repeating from the Zelle textbook:
Analyze the Problem |
Figure out exactly what the problem to be solved is. Try to understand as much as possible about it. Unitl you really know what the problem is, you cannot begin to solve it.
|
Determine Specifications |
Describe exactly what your program will do. At this point, you should not worry about how your program will work, but rather about deciding exactly what it will accomplish. For simple programs this involves carefully describing what the inputs and outputs of the program will be and how they relate to each other. |
Create a Design |
Formulate the overall structure of the program. This is where the how of the program gets worked out. The main task is to design the algorithm(s) (the sequence of steps) that will meet the specifications. |
Implement the Design |
Translate the design into a computer language and put it into the computer. In this course we will be implementing our algorithms as Python programs. |
Test/Debug the Program | Try out your program and see if it works as expected. If there are any errors (called bugs), then you should go back and fix them. The process of locating and fixing erros is called debugging a program. During the debugging phase, your goal is to find any and all errors, so you should try everything you can think of that might "break" the program. |
After you've logged on to a computer in the lab, start WingIDE by clicking on the WingIDE (feather) icon at the bottom of the desktop screen, in the application "dock". Once it starts, you'll see an application with three primary panes. Let's take a quick tour.
The top pane, underneath the menu row and the button row, is the edit area, where files being edited are displayed. Multiple files may be edited concurrently and may be navigated to by selecting the files 'tab'. Right now, the tab should indicate 'untitled', and the editing area should be empty. The lower left pane has multiple tabs for 'Call Stack' and 'Debug I/O', and 'Search', which we will learn more about later. The lower right pane contains a tab indicating 'Python Shell', and the contents of the pane contain an instance of the Python interpreter. The Python interpreter is an area where we can execute Python statements and explore the language. We can execute simple Python sequences here, but if we want to create a program that we can execute again and not have to type in all of the individual statements, we place the Python language statements in a file that we can save in the filesystem and can later retrieve. This is what we will do now with our first program.
Let's get started with our first program. Type the following Python code into the edit area in the top pane of the application. If there is no 'untitled' file tab, first click the 'New' button to create one.
# tempConv.py
# A Celsius to Farenheit conversion program.
# Name: <your name>
# Date: <date>
# Assignment: Lab 1
print "This program will convert a temperature in Celsius"
print "to the equivalent temperature in Fahrenheit"
c = input("Enter the temperature in Celsius you wish to convert: ")
f = c * 9.0 / 5.0 + 32
print "The equivalent temperature in Fahrenheit is", f
Once you're done, you'll want to save this file. In Python, the base name of the file can be anything you wish, and, by convention, we use a '.py' extension so that we can readily recognize Python source files. Per the comment in the first line, we have decided to name this file 'tempConv.py'. It is always a good idea to name the source file something representative of the code within, and we often use the mixed case (called 'Camel case') to concatenate multiple words or abbreviations to make a single name. We avoid using spaces in filenames, although most modern operating systems can handle such names.
Now, where to save the file in the filesystem so that we can find it again? Well, for starters, you should not save it to the local lab machine; even if you get the same lab machine next time, there is a good chance your file won't be there anymore. So you'll want to save it to a network workspace. You should have access to two network workspaces, represented by icons on your workstation desktop. One is your personal workspace. All your work should be saved here. You'll probably want to create a folder on your personal workspace called cs110 or something (and probably some subfolders, since you'll end up with quite a few files by the end of the semester). The other networked workspace is the shared workspace. That is where you'll turn in the files associated with your completed lab at the very end. But we'll get to that in just a bit.
For now, create a folder called cs110 on your personal workspace, and create a subfolder underneath the class folder and name it lab01. Save your current program as tempConv.py in this subfolder. In the 'Save' and 'SaveAs' dialogs, there is a drop down list above the Folders and Files panes that gives the Set of subdirectories to the current save location. You'll want to use this drop down to start by selecting the 'root' of the filesystem, denoted with '/'. Once you have done that, there will be an entry in the Folders pane named 'Volumes/' If you double click this folder, you will see your personal workspace and be able to navigate to it to save the file. The filename goes in the text entry at the bottom of the dialog before you click the 'OK' button. If you need help creating or navigating folders, don't hesitate to ask me or a classmate.
Now for the moment of truth: does it execute and perform the function we desire? With the tempConv.py tab selected in the edit pane, click the 'Run' button at the top of the interface.
The lower right pane, containing the Python Shell, should redisplay and a message 'Evaluating tempConv.py' should appear. If there were any "syntax errors" in the source code you typed in, you will see an error message at this time. Source code syntax errors are also indicated (where possible) with a red underline in the edit pane. You should examine your code carefully and make sure it matches exactly to what is given above.
If there was no syntax error, you should instead see the result of Python executing the first print statements and the input statement in your program. The focus should be in the Python Shell pane and you should be able to type a number, say 100, and the program will then execute the conversion and print the result.
Congratulations! You've just edited, saved, and executed your first Python program!
Examining the program, we see a straightforward sequence. The source begins with five lines that each begin with the '#' character. In Python, this indicates a comment, and comments have no effect on the execution of the program. They are documentation to aid the human reader of the program in understanding what is going on. Comments need not begin in the first position in a line. Everything from the '#' comment character to the end of the current line is ignored by the Python interpreter. As illustrated here, all your programs should include the name of the file, a description of what the program does, your name, the date, and the assignment in comments at the beginning of the submitted file.
We next see a blank line, which also has no effect on the execution of the program. The next two lines are print statements, which provide output from the program to the user, in this case simply describing the purpose of the program. The last three statement lines (10, 11, and 13) illustrate a very typical programming pattern of:
You can now experiment with modifications of your program. Try changing the contents of any of the strings that are printed, including the prompt string in the input statement. Also try changing the 9.0 to 9 and the 5.0 to 5. You are going to turn this program in at the end of the lab, so make sure you change the program back after your experimentation and save it again.
Unlike English or other spoken languages, computer programming languages tend to be very picky. If i were to ignore punctuation capitalization and even have a feww speling misatkes youd prbably stll be abel too underrstnd wht i ment. As you may have already discovered, such is not the case with programming languages. When the code you create doesn't match the syntax that Python understands, the interpreter will generate error messages. These error messages often are a bit arcane and can be difficult to parse. In this part of the lab, we'll deliberately generate a few Python errors in your tempConv program, as well as directly into the Python interpreter. This should emphasize just how picky Python is, while also giving you some familiarity with what some of the error messages mean.
Open tempConv.py in the WingIDE if it isn't already open. So as not to ruin your first program, first we'll make a copy by saving it under a different name. Using the 'Save As' option under the 'File' menu, save the current file as tempConvError.py in the same location on your personal workspace. Begin by making sure it still works as before by executing the program and making sure you get the correct output.
For each of the modifications below, you'll make the specified change and try executing the resulting program. For some changes, you will generate a syntax error. In this case, the error message will include a line number where the error was discovered by the interpreter. In other cases, the program will execute, but you may or may not get the 'correct' answer. In this case the bug is referred to as a logic error and Python is doing _exactly_ what you tell it to (through the source code), but the problem is that we have not told it the right thing.
Your task is to make a list of the error messages or identify the logical error condition along with the changes that generated them. This list should be saved in a text document as errors.rtf (use TextEdit to create this file). Once you've generated error messages and recorded them, revert to the working version of your program and move on to the next one.
Capitalize the 'p' in the first 'print' statement.
Remove the '#' in the 'Assignment' comment (but leave the space before the word Assignment).
Repeat 2., but this time remove the space as well.
Add a line containing the sequence of characters "hello" following the last comment line (including the double quotes).
Repeat 4., but without the double quotes.
Capitalize the word 'input' on line 10.
Change the word 'input' to 'raw_input' on line 10.
Misspell 'Fahrenheit' in the final print statement.
Replace the double quotes with single quotes in the final print statement.
Change '9.0' to '9' and '5.0' to '5' in the conversion statemment, and use the value '1.0' as input.
Repeat 10., but this time use the value '1' as input.
For example, your first entry should read something like this
Problem | Error message(s) |
Capitalized the 'p' in first 'print' | invalid syntax: <string>, line 7, pos 58 |
Create a new program, called tempConvLoop.py that satisfies the following specification:
Using a definite for loop, print a table of Celsius/Fahrenheit equivalences. Let c be the Celsius temperature, and let c range from 0 to 100. For each value of c, print a line with both the value of c and the corresponding Fahrenheit temperature.
As with all subsequent labs and prelabs, you'll be saving the final version to your assignment inbox on the shared workspace. On the shared workspace, you should see a folder labeled BRESSOUD, and in that, a folder for our class. From there go to your assignment inbox and under the folder with your name, create a folder called Lab01. Copy the final versions of your solutions to this file. For this lab, there should be 3 files:
No other files should be loaded to your assignment inbox. If you have other files you'd like to keep, they should be kept on your personal workspace.
That's it! Make sure these files have been saved to this workspace by the due date for this assignment before class starts.