We'll use the IDLE Python programming environment for this tutorial. IDLE has an editor like Notepad. 1. On the USF lab machines, you can access IDLE from the Windows: All Programs | Python 2.5 | IDLE 2. In the bottom panel of IDLE, you can enter Python commands directly, one at a time. Type in the following lines to see what happens (hit return after each line) print hello 8*3 x = 7 y = 9 z=x*y print z 3. Now let's write a program in a file, and run the entire program at once. Select File | New and open a new program file. In the new file, enter the following code all the way to the left (don't indent): principal = 3000 4. Save your program in interestRate.py on your H: in a sub-direcotory named pythonSamples. 'py' is the extension we give Python programs.interestRate = .1 oneYearInterest = interestRate*principal print 'interest is:', oneYearInterest 5. Select Run | Run Module. The output for the program should appear in the other IDLE window. You should see the output: interest is: 300.0 6. Now go back to the text editor and modify your program so that it asks the end-user for the principal and interestRate. You can ask the end-user of your program for data with the 'input' function. For example, principal = input('please enter the principal:') When an 'input' statement is executed, the user is prompted to enter a value, and that value is put in the variable on the left-hand side of the statement. You'll need a similar statement to get the interestRate from the user. 7. Re-run your program. This time, the user (you) should be prompted to enter the principal and interest rate, and the one year interest displayed should be dependent on what the user entered. 8. Let's make one more change to your program. First, SaveAs your program to 'compoundedInterest.py'. In this program, instead of printing out the interest, print out the principal that is accrued after one, two, and three years. Make these changes in the editor, then re-run the program to test it. 9. When you have your programs working correctly, create a page on your Google Sites portfolio for Python Code Samples. On that page, copy and past the code for your program, and provide a short explanation of how it works. Also, attach the actual .py file to the Google Sites page. |