cs 110 Homework 01
Welcome to your first lab class! In this lab you will get to experiment with the Python interpreter, familiarize yourself with WING (the editor you will use to write programs), and write your first complete Python program.
Step 1: Creating directories
On your desktop, look for your Personal workspace and double click to get a Finder Window with the contents of your personal workspace displayed. You may have to navigate to 'Student Personal Workspace' underneath the 'Personal' mounted filesystem.
Troubleshooting: If you do not have a Network Disk called 'Personal' (or 'Student Personal') displayed on your desktop, you may have to establish a connection to the Novell Network File System server. To do this, we select 'Go'->'Connect to Server ...' from the Finder's main menu. From there, enter
cifs://proteus.cc.denison.eduin the 'Server Address' input of the resultant dialog. From there, you should be able to select 'PERSONAL' as the volume to mount. Then navigate so the contents of your 'U'/Personal drive is visible by double clicking the folder named the same as your login.
Within this space, create a directory called cs110. Inside your
cs110 directory create another called hw01 to save your work
from this lab into. Be
Step 2: Predicting Python Code Behavior
Click the following link to a PDF file with the printout of a Python program named guess.py. This should bring up the 'Preview' application and enable you to print the program code. Do so, printing to the Olin217 printer and release the print job.
Look at the Python code and do your best to figure out what
each line/step of the guess.py
program does.
Opposite each line of the program, write your prediction
down. ... so you are describing the semantics from the
point of view of the Python interpreter that must execute the
lines of code. If you are not sure what a line does, that
is ok, but really think about it and make your best guess.
Put you name on the sheet and hand in your annotated guess.py.
Step 3: Creating a Python File for a Program
Our goal in this step is to create a text file on disk whose contents consist of the guess.py python program. We will use the editor component of the WindIDE for this purpose. Open WingIDE (the feather icon program) as you did in class.- Use 'File->New' or the leftmost toolbar button. The result should be an empty file (a file with no content) displayed in the upper panel of the WingIDE, probably creating a new Tab in that panel over the Help Tab that was already there.
- Use 'File->Save As ...' or 'File->Save' or the third (save) button on the toolbar in order to save the text file to the filesystem.
- In the resultant dialog, navigate to the directory
and at the bottom of the dialog, enter guess.py for the filename.
print "Welcome!"Save your edits. Once the file is saved, you can run it by clicking the Run button. If, after a line is entered and you have moved the cursor to another line, you see a wavy red underline, then you have a syntax error that must be corrected. Check again for an exact match to the given code.
guess = input("Guess the number: ")
if guess == 5:
print "You win!"
else:
print "Sorry, better luck next time."
print "Game over"
Try running your program at least 3 times, and when prompted, enter 2, then 7, then 5 during each run. Congratulations! You (hopefully) have your first running program.
Self assess the accuracy of your predictions from Step 2.
Did you predict that 'Game Over' would always be printed,
regardless of the value typed by the user? Include your
answers to these questions in a TextEdit document named
report.rtf. You will be adding to this report in step 5
below.
Step 4: Documenting your code
Add the following lines to the beginning of your guess.py program:
# guess.py # Number guessing game # Author -- (enter your own name) # Date -- (enter today's date)
In addition, add comments to the rest of the program,
explaining each of the lines. You can either add a comment as
its own line prior to the program line or you can add it on the
same line as the code (everything to the right of the # will be
ignored by the interpreter). Try and make the result as neat as
possible. Rerun the program to make sure everything still
works properly.
Step 5: Messing it up
Now that you have a working program, let's see what happens when you deliberately add some errors into it. The idea is to learn the error messages produced by the interpreter so that when you get errors in more complex programs in the future you can understand what's going on. Add the following errors into your program, making sure to
revert to the correct version before introducing the next error.
Report (in your own words)
the error that occurs in each case in a file called report.rtf (using the
TextEdit program). For each error, you should also categorize
the error as a syntax error, runtime error, or a
logical/semantic error, or no error.
- Capitalize the 'p' in the first 'print' statement.
- Capitalize the 'i' in the 'input' statement.
- Change the '==' on the line beginning 'if' to '='
- Remove the '#' (and space) in the 'Date' line comment.
- Add "hello" (including the double quotes) on a line by
itself following the final 'print' statement.
- Same as in step 5, but without the double quotes.
- Remove the ':' following the 'else'
- When the program is run, on separate runs, enter real
values of 2.0, -1.5, and 5.0 when prompted for input.
- When the program is run, enter the character g when prompted for input.
- After the "Welcome!" print statement, add a line 'g = 5'
(without the single quotes) and then run the program and enter
the character g when prompted for input.
Step 6: Writing your own program
Okay, time to try your hand at writing a program all of your own. Write a program yearsToSeconds.py that gets as input from the keyboard an amount of time (in years) and prints to the screen the number of seconds in that period of time. To make things simple, you may ignore the existence of leap years. Remember to add documentation at the top (your name, the date, what the program does).Once you have written the program (and it runs without any errors) try it out on the following inputs, and verify that it is working correctly:
- 0
- 1
- 10
- 15
- 30
Step 7: Identifying Design Improvements
Suppose you are the creator of the number guessing game, guess.py,
begun earlier in this homework, and you have made it into an
iPhone app and put it on the Apple App Store. Your reviews
are in, and, while your users like the basic concept of the number
guessing game, they think it can be improved.In your results.rtf file, think like a user might think, and identify at least two, and preferably three, specific improvements that would make the game better. ... and a better user interface will not count as one of these improvements. Visit your professor during office hours and share your ideas. For one idea, consider "improved messages" that could be reported to the user depending on their guess. ... if you were playing the game, what might be better messages for the following user entries:
User
Entry |
"Better"
message |
3 |
|
0 |
|
7 |
Submitting your lab
Email me (check the course website for my email address) the following files by class time on January 26th:-
guess.py
-
results.rtf
-
yearsToSeconds.py
-
betterguess.py (if you implement any design improvements)