IT 240 Homework One - DePaul University



CSC 401 Assignment FIVE? ?? Out of 40 pointsDue Date: Wednesday, Feb. 12 by 11:59 PM? Rememer to spend a LOT of time practicing with the materials before beginning the assignment. You should be comfortable opening files (including using FileNotFoundError exception handling), and using the 3 read functions we talked about. Do this BEFORE beginning the assignment!TIPSA key strategy for this assignment is doing small pieces one at a time. For example, for problems that require you to work with a file, FIRST make sure you can simply open the file, reading the first line of text and outputting it to the screen. THEN worry about exception handling for the file. THEN worry about doing the first requirement on the assignment. And so on. Do not try to do everything at once. This is KEY. You do NOT have to do all the requirements of each question in the order in which they are written. For example, you can do exception handling last – even though the question mentions it early. Or do the exception handling FIRST and make sure it works. But the key key key point is to do one little task in a problem at a time and make sure it works. Only then should you move on to the next little task in the problem.Efficiency: Remember that you want your code to be efficient. The file ‘shakespeare.txt’ which you will be using in your assignment is pretty large. So, for example, you should not open the file more than once in a function. The file should be opened a maximum of ONE time in a given function (for this assignment). Never more than once.Also: Do not forget to close your files when you are done reading from them! Submission????????? Save your program to a file labeled as yourName_A5.py ????????? Upload your file to the Submissions folder in D2L. It is okay to work with someone together on your assignment. However, be sure that you indicate that you did so as a comment at the top of your file.Remember to put your functions at the top of your file. That is, you can only invoke a function inside your script below the point where the function was defined. If you invoke a function above the line where the function was defined, your code will probably not work. All functions must include a brief docstring. You may run into situations where the length of a line is longer than 79 characters. If you can, try to break up the line. However, do NOT spend a lot of time on this. This is not something I want you to spend a lot of time worrying about right now. Focus on the more important Python stuff first.Problems?Problem #1Create a function called?numLinesInFile()?that takes 1 argument representing a file name. The function should count the number of lines in the file and return that value. Your function should use exception handling to make sure the file is present. If the file is not present (e.g. if the user has a typo in the filename), then your function should output: “Sorry, I could not locate your filename.” and the function should end. print(numLinesInFile('example.txt')) #should output 4print(numLinesInFile('shakespeare.txt')) #should output 147837print(numLinesInFile('Bakespeare.txt')) #should output: "Sorry I could not find your file."#The above line of code will also probably output ‘None’#Bonus: 3 additional points if you can explain why it outputs ‘None’?Problem #2Create a function called?searchShakespeare()?that takes 1 argument representing a list of strings.Inside of this function, you will search the contents of the complete works of Shakespeare (shakespeare.txt) for the terms that are contained in the list. You can download the ‘shakespeare.txt’ document off the class web page. Note the comment about the ‘encoding’ argument. Recall that 'encoding' is an optional argument to the open() function. You may or may not find that you need to include this argument when you invoke the open() function.Place these words in a list. Feel free to paste this list:??['Hamlet','Beyonce','thou','wherefore art thou romeo']Your code should open the file, read in the contents into a string, and then close the file. Using a??loop?to iterate through the list, output how many times each of these terms is present in the Shakespeare document.Your search should not be case sensitive. That is, 'Hamlet' should match 'Hamlet', 'HAMLET', etc.? There are different approaches to doing this. Hint: Think of the string functions upper() and lower(). Either one of those can help you accomplish this task.Output the number of times each string in the list appears in the Shakespeare document. Your output should match mine below. That is, it should say (using 'Hamlet' from the list as an example):??The word "hamlet" appears 475 times in this document.?You must use a format string to do this. ?You will note that in my example, 'hamlet' is in lower case. That was part of my algorithm (i.e. strategy) for making this search insensitive to case. NOTE: You should have double quotes around the word that was searched for (i.e. “Hamlet”). However, this is one of those times when you should worry about this little detail only AFTER you have gotten the rest of the code to work. Remember to only worry about one requirement at a time. You must use exception handling that catches a FileNotFoundError exception if the file can not e found. If this happens, you should simply output: “Sorry, I could not locate the file.” (You can test your exception handling by mistyping the filename, (e.g. Bakespeare.txt) in your file. Here is the output from my run:lstWords = ['Hamlet','Beyonce','thou','wherefore art thou romeo']searchShakespeare(lstWords)The term "hamlet" appears 475 times in this document.The term "beyonce" appears 0 times in this document.The term "thou" appears 8266 times in this document.The term "wherefore art thou romeo" appears 1 times in this document.?Problem #3This problem is fairly similar to the last problem in that some of the functionality is exactly the same. Create a function called?shakespeareWordSearch()?that takes 0 arguments. Inside the function, ask the user if they want to search for a word. Tell them they should type 1 for yes and 2 for no. If the user types 1, your function should output the number of times the word appears. If they type 2, simply say “Goodbye” and end the function. When the user enters 1 or 2, you will need to cast the number to an integer (recall the ‘int()’ function). HOWEVER, if the user types a string (e.g. One, or Two, or if they type ‘Yes’ or ‘No’) then Python will generate a ValueError exception. In your exception handling block, your program should output something like: “Please enter the number 1 or 2. Goodbye!” Then end the function. If the user does type the number 1, then your program should simply output the number of times the word appears in shakespeare.txt. If the user types the number 2, then simply output ‘Okay, goodbye!’As before, be sure to use exception handling code when opening the shakespeare.txt file. (You should use exception handling code WHENEVER you try to open a file). However, because you (the programmer) are writing the name of the file inside your function yourself (i.e. the user is not typing the name of the file), then that exception should not be generated.Here are some sample runs:shakespeareWordSearch()Would you like to search for your own term?Enter 1 for yes and 2 for no: OnePlease try again and enter only a number!Have a nice day!shakespeareWordSearch()Would you like to search for your own term?Enter 1 for yes and 2 for no: 2Okay, goodbye!shakespeareWordSearch()Would you like to search for your own term?Enter 1 for yes and 2 for no: 1Enter a word: Chicago BullsThe word "chicago bulls" appears 0 times.shakespeareWordSearch()Would you like to search for your own term?Enter 1 for yes and 2 for no: 1Enter a word: RomeoThe word "romeo" appears 313 times. If you have any questions regarding this assignment, please post them to the COURSE Discussion Forum. You may discuss strategy and approaches, but please do not post any solutions! ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download