


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



This assignment asks you to write three simple programs in Python. We will provide step by step guidelines for writ- ing the first program, and some tips for writing the other two programs.
As before, you can do this assignment either in the computer lab or on your own computer, if you have one. In the lat- ter case, you will need to install Python. To do this, go to the web page http://www.python.org/download, download the appropriate installer, and run it.
On the lab computers, go to the Start menu, choose “All Programs”, then “Class Software”, then “Python 2.5”, then “IDLE (Python GUI)”. If you are on another computer, you may have to look elsewhere, depending on where you installed it. Look for the IDLE application, and execute it (by double-clicking).
What you see is the Python interpreter. Try typing ‘print “hello world”’ as follows (note: “>>>” is the prompt that the computer prints):
print "hello world" hello world
You just wrote a very short program to print something.
We would like to store longer programs in a file so that we do not have re-type them each time we want to use them.
This program should print out your name, the date, and then computer and print out a binary representation of an inte- ger between 0 and 31 inclusive. For this exercise, you can print the binary digits in reverse order. The name of file should be “binary.py”. What follows are step-by-step instructions to help you write this program.
a. Go to “File” on the Python GUI b. Select “New Window”. c. Now you should see a new window pop up. This is the script window, where you write your collection of Python commands. Important : At this point, you should work through all the things described in pages 8–11 of the book. Also, please read pages 28–50. This lab will be easier if you do the reading before you try it.
a. In the script window, use two print statements to type out your name and today’s date, using the wording of the example after 2.3. For example, my first line would be: print “My name is Matt Bishop” As you type your statements, “print” will be in a different color from the quoted parts. The Python GUI uses dif- ferent colors (called “syntax highlighting”) to help programmers differentiate between reserved words of the Python language (such as “print”) and everything else. b. When you are done writing the two statements, save the file on the Desktop as binary.py c. Select the “Run” menu, then “Run Module” (or press F5) to run your program. If you did things right, your output should look like this (of course, your program will display your name, and today’s date):
My name is Matt Bishop Today is 5/11/
If you made a mistake in typing (a syntax error), the interpreter will highlight the offending part of the line in red. Before continuing, we want to you to learn about two common error messages. d. Create an error in a reserved word by changing one of your print statements to “Print” (that is, capitalize the first letter of the word “print”). Then press F5 (and agree to save your program). The interpreter will notify
you that you have a syntax error. Click “OK”, and look at the program. It will highlight the end of the line where you changed “print” to “Print”. This demonstrates that the interpreter can guess where the error is located, but may highlight an area a little after the actual error. Once you see this, please change the “Print” back to “print”, and then press F5. Be sure your program runs properly! e. Now, create an error in a non-reserved word by deleting the double quote in front of “My” on the line that prints your name. You should only have one quotation mark at the end of the line. Then press F5 (and agree to save your program). The interpreter will again report that you have a syntax error, and will highlight “name”. This again demonstrates that the interpreter can guess where the error is located, but may highlight an area a little after the actual error. Now, please add the missing double quote, and then make sure your pro- gram runs properly
My name is Matt Bishop Today is 5/11/ My number is 28 0 0 1 1 1
This program asks the user to type some text. It then prints what the user typed in quotation marks, followed by what
the user typed with all vowels (the letters “a”, “e”, “i”, “o”, and “u”) capitalized^1 and in quotation marks. The name of your program file should be capvowels.py. Your prompts and output should match the example below. We will not give you step by step direction for this program, but will provide some hints.
Here are some sample runs. Remember, when given the inputs following the word “Price: “, your program must give exactly the same output as this example.
Price: 100 For 100.0 a 15% tip is 15. For 100.0 a 20% tip is 20.
================================ RESTART ================================
Price: 39. For 39.99 a 15% tip is 5. For 39.99 a 20% tip is 7.
================================ RESTART ================================
Price: 23. For 23.86 a 15% tip is 3. For 23.86 a 20% tip is 4.
To hand everything in, create a ZIP file named lab7.zip containing the three python files. In the labs, you can use the FilZip utility to create the ZIP file. Go to Start, then to All Programs, then to Utilities, then to FilZip. In FilZip, go to File, then to New Archive, and create lab7.zip on the Desktop. Click the Add (+) button and select the files. Note that you must click two Add buttons before they are added to the archive. Save your zip file in MySpace, and also submit it to MyUCDavis.