Homework 2-3

Due October 13, 2015, 9:00 am


If a problem is marked as “(Independent)”, you may only discuss the problem with course staff. Otherwise, you are free to discuss the concepts that will help you solve the problems with classmates as well as course staff. However, you are never allowed to simply copy answers.

Task 1

Create a folder on your desktop called “HW2-3”. Download the starter program moby-starter.py and MobyDick.txt, the text of Moby-Dick, from the course website and save them in this folder. Now open IDLE and use File > Open... to select your Python file. For this task, you will add a couple new functions to this file. When you write your functions, remember to write down what your functions does and what the arguments mean by commenting your code. Remember that comments start with a #.

  1. The starter program contains a function called printMD1000 that takes no arguments, and is supposed to print out the first one thousand characters in Moby-Dick. It needs to know where to find the MobyDick.txt on your computer, however. Modify the string 'C:\\WinData\\Desktop...' so that its value is the correct path to MobyDick.txt on your computer. On Mac, the path to the file might look something like this: /Users/hmendes/Desktop/HW2-3/MobyDick.txt. Remember, you can right-click a file and get its location in both Windows ('Properties') and Mac ('Get Info'). This will give you the folder that the files lives in, so you'll still need to add the file name to the end of that path. Also keep in mind that in Windows, since the file path is separated by backslashes, which are escape characters in Python, you should use double backslashes in the file path.
  2. Look at the rest of the code for printMD1000 and make sure you understand why it should work. Then press F5 to run it. You should see another IDLE window come up. Then, after the prompt, type printMD1000() to call your function. Make sure it produces the correct answer. (You do not have to hand in anything for this part.)
  3. Now, write another function called print1000 that takes one argument. Suppose in the folder “HW2-3”, I have bunch of text files: MobyDick.txt, Hamlet.txt, Bible.txt, etc. You want the argument to this function to tell the function which text file to print. For example, calling print1000('MobyDick.txt') should print the first 1000 characters of the file C:\WinData\Desktop\HW2-3\MobyDick.txt, and calling print1000('Hamlet.txt') should print the first 1000 characters of the file C:\WinData\Desktop\HW2-3\Hamlet.txt, assuming the file does exist. On your computer, the path to the HW2-3 directory will probably be different than C:\WinData\Desktop\HW2-3; use the path that works on your computer instead. Hint: remember you can use the + operator to “glue” strings together. Note that you should include a short description of your function and what it takes as input, similar to the descriptions provided in printMD1000().
  4. Now you're going to test the function you just wrote. Try making a new plain text file (ending with .txt) and paste in some text, even if it's just a copy of the "Moby Dick" text. Make sure there are at least 1000 characters in this new file. Now, provide one example of a call to print1000 to this new file, and verify that it works as expected.
  5. Write a third function, printN, that takes two arguments. The first one indicates which file to print (same as the last function) and the second is a number indicating how many characters from the beginning to print. For example, calling printN('MobyDick.txt', 500) should print out the first five hundred characters in the file located at C:\WinData\Desktop\HW2-3\MobyDick.txt. Make sure to include a short description of your function and what it takes in as input.
  6. Provide two examples of calls to printN that show that it works as expected. What are you looking for when you check that printN is correct?

When you're done, name your program FirstLast_MobyDick.py — for example, AlexandraPapoutsaki_MobyDick.py.


Share FirstLast_MobyDick.py with cs0931handinfall2015@gmail.com .

Note: Before you turn in your Python files, make sure they run without any errors (save your Python file, then select Run > Run Module or hit F5 on your keyboard)! If nothing appears in the Shell, don't worry, as long as no red error messages appear. If they don't run, i.e. if red stuff starts appearing in the shell, points will be taken off!