CS 105: Problem Set 4
due by 11:59 p.m. on Friday, February 19
Preliminaries
In your work on this assignment, make sure to abide by the expectations for academic conduct in this course. Don't forget that looking at another student's work or showing your work to another student is not allowed.

If you have questions while working on this assignment, please come to office hours or email
cs105-staff @ cs . bu . edu (removing the spaces).

Overview
In this assignment, you will become familiar with the Python programming language, the Python interpreter, and the IDLE integrated development environment.

Getting Started
In order to begin working on this assignment, you will need a way to access the Python interpreter and IDLE. These tools come preinstalled on at least some Linux and Macintosh OS X machines. If you use a Windows PC or if your computer doesn't have both Python and IDLE, you can download the necessary file from the following page: http://www.python.org/download.

For Windows, click on the link labeled Python 2.6.4 Windows installer (the filename is python-2.6.4.msi) and save the file to disk. Once the download has completed, doubleclick on the Python-2.6.4 icon on your Desktop (or wherever the file was saved) to run the installer. You should be able to use all of the default options during the installation.

For Mac OS, click on the link labeled Python 2.6.4 Mac Installer Disk Image (the filename is python-2.6.4_macosx10.3.dmg) and save the file to disk. Once the download has completed, doubleclick on the icon for the downloaded file on your Desktop (or wherever the file was saved) to run the installer. You should be able to use all of the default options during the installation.

Once Python is installed, you are ready to start working on the assignment.

(Note: it is also possible to use Python and IDLE on the UNIX or Windows machines in the undergraduate lab.)

Part I: Experimenting with the Interpreter (20 points total)
This part of the assignment requires you to experiment with the Python interpreter interactively, and to answer questions based on your experiments.

Put your answers to these problems in a text file named ps4_partI.txt. Don't forget to save the file as a plain-text document.

You should begin by starting up the Python interpreter shell -- either the command-line version, or the one that you get when you start up IDLE. You should see the following prompt:

     >>>
which indicates that it is waiting for you to enter a command.

In the questions below, you will experiment with operators and built-in functions by evaluating a number of expressions. Recall that you can evaluate an expression by just entering it at the command prompt in the interpreter. Make sure that you do not include the expression letter (e.g., a or b) when you enter an expression!

  1. The + operator, part I (3 points)
    What results do you obtain when you evaluate each of the following expressions in the interpreter? Include the results in your answer to this problem. Make sure that you include the single and double quotes when you enter an expression that includes them. Some of these expressions may produce an error. If that happens, describe the error that you receive.

    1.     1 + 2
    2.     1.5 + 2.5
    3.     'apple' + 'ball'
    4.     'apple' + 2
    5.     1 + 'wall'
    6.     "Python" + ' is ' + "fun"

    Based on the results above and any additional experiments that you wish to conduct, briefly describe your conclusions about how the + operator works for different types of data.

  2. The + operator, part II (3 points)
    Let's say that you have executed the following commands.
         apple = 5
         ball = 7
    
    Explain the difference between the following two expressions:
         'apple' + 'ball'
         apple + ball
    
    While you are welcome to use the interpreter to see what these expressions produce, we encourage you to try to answer the question before you use the interpreter!

  3. The * operator (3 points)
    What results do you obtain when you evaluate each of the following expressions in the interpreter? Include the results in your answer to this problem. Make sure that you include the single and double quotes when you enter an expression that includes them. Here again, some of these expressions may produce an error. If that happens, describe the error that you receive.

    1.     20 * 6
    2.     'oh' * 2
    3.     'oh' * 7
    4.     "wow!" * 10
    5.     'apple' * 'ball'
    6.     5 * 'oh'

    Based on the results above and any additional experiments that you wish to conduct, briefly describe your conclusions about how the * operator works for different types of data.

  4. The cmp function (3 points)
    The cmp function takes two arguments and returns a numeric value that indicates how the arguments compare -- i.e., whether the first argument is less than, greater than, or equal to the second argument. What results do you obtain when you evaluate each of the following expressions in the interpreter? Include the results in your answer to this problem.

    1.     cmp(5, 10)
    2.     cmp(10, 5)
    3.     cmp(5, 5)
    4.     cmp(10, 10)
    5.     cmp('apple', 'ball')
    6.     cmp('ball', 'apple')
    7.     cmp('apple', 'apple')
    8.     cmp('ball', 'ball')

    Based on the results above and any additional experiments that you wish to conduct, briefly describe your conclusions about the meanings of the values that this function returns.

  5. The range() function, part I (4 points)
    As discussed in lecture, the range() function is a built-in function that can be used to generate a list containing a sequence of numbers. It can take one, two, or three arguments.

    What results do you obtain when you evaluate each of the following expressions in the interpreter? Include the results in your answer to this problem.

    1.     range(2)
    2.     range(8)
    3.     range(6, 12)
    4.     range(20, 25)
    5.     range(0, 24, 3)
    6.     range(0, 24, 4)
    7.     range(10, 5, -1)
    8.     range(30, 15, -3)

    Based on the results above and any additional experiments that you wish to conduct, briefly describe your conclusions about how the range() function works when given one, two, and three arguments.

  6. The range() function, part II (4 points)
    As discussed in lecture, the range() function is often used in conjunction with a for loop. The for statements below use an explicit list of numbers (e.g., [0, 1, 2, 3, 4]). Let's say that we want to replace these explicit lists with expressions involving the range() function. What range()-function expression would be needed in each case?

    1.     for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
              print i 
      
    2.     for i in [20, 18, 16, 14, 12, 10]:
              print i
      
Part II: Working with Programs Stored in Module Files (20 points total)
You should use IDLE for this part of the assignment.

  1. Savings account calculator (12 points total)
    The program below is supposed to determine the percent change in a person's savings account that results from two financial transactions (deposits and/or withdrawals). The current version assumes that the person starts with a balance of $1000. It reads the two deposit or withdrawal amounts from the user (a withdrawal would be specified as a negative value), and then it is attempts to compute and print the percent change in the account (the total change in the account balance divided by the starting balance and multiplied by 100).
        start = 1000
        change = 0
    
        for i in range(2):
            amount = input("Enter a deposit or withdrawal amount: ")
    	change = change + amount
    
        perc_change = 100 * change / start
        print "The percent change in the account is", perc_change
    
    We have put this code in a module file called accountCalculator.py. To download it, click here and save the file to your computer using the File->Save As (or equivalent) option in your browser. Once you have done so, open the file in IDLE by using the File->Open menu option. Then you will be ready to work on the following problems.

    1. (3 pts) There is a bug in the current version of the program. To see this, try running it several times using the Run->Run Module menu option. Although it does work for some combinations of transactions (e.g., 100 and 200, for which it reports a 30 percent change), it does not work for others (e.g., 100 and 55, for which it reports a 15 percent change, when the precise change is 15.5 percent). Fix this bug by making a single change to the code that we have given you.

    2. (2 pts) Change the program so that it handles 4 transactions, rather than just 2.

    3. (2 pts) Change the program so that it prints the word "percent" and a period after the percent-change value.

    4. (3 pts) Change the program so that it prints the new account balance, in addition to the percent change. The new balance should go on a separate line before the percent change, as follows:
          The new account balance is 1200 dollars.
          The percent change in the account is 20 percent.
      

    5. (2 pts) Change the program so that it prints a blank line between the last value entered by the user and the line that reports the result of the computations.

    Make sure that all of your changes to the module are saved before you move onto the next problem.

  2. A more flexible account calculator (8 points total; 2 points each part)
    We'd like to make our account-calculator program more flexible by allowing the user to specify both the starting balance and the number of transactions.

    You should make these changes in a separate file. To do so, save a copy of your accountCalculator.py file from the previous problem by using the File->Save As menu option in IDLE. Give the copy the name accountCalculator2.py. Make sure to include the .py extension at the end of the file name.

    1. Begin by adding an input statement that asks the user to enter the starting balance and assigns the user's input to the variable start. Use an appropriate prompt, making sure that there is a space at the end of the prompt. This input statement should replace the existing assignment statement for the variable start.

    2. Next add an input statement that asks the user to enter the number of transactions and assigns the user's input to a variable. Use an appropriate prompt with a space at the end of the prompt, and use a descriptive variable name that makes it clear what the variable represents.

    3. Next, make whatever changes are needed so that the program reads in the specified number of transactions.

    4. Finally, modify the program so that the prompts that ask the user to enter a transaction include the number of the transaction that should be entered next. Here's what the prompts should look like:
          Enter transaction 1 100
          Enter transaction 2 200
          ...
      
      Note that the prompts should remain on the same line as the user's inputs (which are shown in bold above). In addition, make sure that the first transaction has the number 1, the second transaction has the number 2, and so on. Hint: Instead of specifying the prompt as part of the input statement, use a separate print statement that comes before the input statement, and change the input statement so that it does not take an argument, as follows:
          amount = input()
      

Submitting Your Work
You should use WebSubmit to submit the following files:

  • your ps4_partI.txt file
  • your accountCalculator.py file
  • your accountCalculator2.py file

Here are the steps you should take:

  • Log on to WebSubmit.
  • Select hw04 from the Homeworks... drop-down menu and click the corresponding Go to project page button.
  • For each file that you want to submit, click a Choose File button, and find and select the file. Once all of the files have been chosen, use the Upload Files button to submit them.
  • After the files have been uploaded, links for the files should appear near the top of the project page. Click on the link for each file to view it so that you can ensure that you submitted the correct file. We will not accept any files after the fact, so please check your submission carefully.

Note: WebSubmit occasionally becomes inaccessible because of problems with the University's password server. If you encounter problems, close your browser and start again, or try again later if you still have time. If you are unable to submit and it is close to the deadline, email your homework before the deadline to cs105-staff @ cs . bu . edu.