IT 240 Homework One - DePaul University



CSC 401 Assignment SIXOut of 40 pointsDue Date: Wednesday, Feb. 19 by 11:59 PM? Rememer to spend a LOT of time practicing with the materials before beginning the assignment. Remember to try creating a strategy (i.e. an algorithm) and then writing Pseudocode BEFORE writing any Python code. When it comes to outputting to a file, I usually start by just printing to the screen. That way I don’t have to keep opening the file to check. However, only do this with SMALL outputs. If you have output that you think will be thousands of lines long, I would not print to the screen! Once you know your program is giving you the output you expect, THEN modify the code to output to a file.Submission????????? Save your program to a file labeled as yourName_A6.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.All functions must include a brief docstring. Problems?Problem #1Create a function called?getNames()?that takes 0 arguments. The function should print a statement saying: “Enter a name and I will enter it to a file. Enter 0 when you are done entering names.” Then all you need to do is use a while loop to keep allowing the user to enter names. Each time they do, append that name to a list. When the user types 0 meaning they are done entering names, at that point take all of the names from the list and enter them into a file called best_friends.txt. You should enter each name on a single line. Also, the name should be in the format: Bob, Lisa, Jane, etc. That is, the first letter is capitalized, and the rest of the letters are not. There is a convenient string function called capitalize() that does just that. Enter a name and I will add it to a file. Enter 0 when you are done entering names.Enter a name: robeRtEnter a name: LISAEnter a name: janetEnter a name: fRaNkIeEnter a name: 0Here is the output in the file:RobertLisaJanetFrankieException Handling Code: For the next problems, you are going to do some exception handling that that is the same. However, I would do this part LAST. That is, take care of the other requirements of the problem, and then when that is done, try to get the exception handling code to work for you. This exception handling code was discussed at the very end of our lecture. This code is around page 9 of the lecture notes L6.docx. It is in the section called: “Putting some key things together…”What this code does is it prompts the user to open a file, but if the user enters a file that does not exist, the code keeps looping until the user gives a proper filename. Your code will use almost the exact same code from the notes, however, you are going to find that one or two very small adjustments need to be made to get it to work properly. The difference comes from the fact that I am requiring you to pass the file to be opened to the function as an argument. (You will see what I mean when you read the problems below). So the first time you try to open the file, you must use the argument that was passed. ONLY if the argument is not a proper filename should you prompt the user to enter another filename. Again, it’s a small change, but something you will need to play around with for a minute.One other minor change you will wan to include is the ‘encoding’ parameter when you try to open the file. Recall that some files require encoding='utf-8' in order to open. Again, I would do all of this only after you have finished the rest of the problem.So to reiterate: Do the next problem (problem #2) without the exception handling code. Get the problem to work. THEN go back and do the exception handling code required. The GOOD news is that once you get it working for problem #2, the code is exactly the same for the remaining problems!Problem #2Create a function called?findNumberOfLines()?that takes 1 argument representing a filename. Inside your function first make sure the file can be opened. If it can not, you should have exception-handling code (discussed above) in which you tell the user that the file could not be opened and that they should try again. Keep making them enter a filename until they give you one that works. The function should simply determine how many lines are in the file, and then print that value. This should not be difficult at all! Make sure you review the functions available to you for reading in from a file! When you print the result, use a format string. Part B: As a block quote (i.e. using triple quotes), below your function (i.e.not inside the function) write the PSEUDOCODE that you used to do this problem. Remember that pseudocode does NOT involve much Python code. It may look slightly like it, but it should show that you have thought through how to solve the problem BEFORE typing any code. Here is the output from my run:>>> findNumberOfLines('flexample.txt')The file "flexample.txt" could not be opened.Please try again.Enter a file to be opened: The file "" could not be opened.Please try again.Enter a file to be opened: example.txtThis file has 4 lines.?Problem #3Create a function called?countWordsPerLne()?that takes 1 argument representing a filename. As with problem #2, inside your function first make sure the file can be opened. If it can not, you should have exception-handling code (discussed above) in which you tell the user that the file could not be opened and that they should try again. Keep making them enter a filename until they give you one that works. Your function should count the number of words on EVERY line. You should then output that value to a file called ‘hw6_p3.txt’. In addition, inside your output file you should specify the line number. For example: Line #0 has 12 words.Line #1 has 7 words.Line #2 has 0 words.ETCAs always, use a format string to do this output. Hint: You will need to use the split() function.PSEUDOCODE: Again, below your function you should write the pseudocode that you used to solve this problem. Here is a sample run:>>> countWordsPerLine('frexample.txt')The file "frexample.txt" could not be opened.Please try again.Enter a file to be opened: example.txt Here is what the file hw6_p3.txt looks like using our example.txt file:Counting number of lines in example.txt.Line #0 has 5 words.Line #1 has 5 words.Line #2 has 0 words.Line #3 has 8 words.Note that the first line of the file shoud say: “Counting the number of lines in example.txt”. Obviously, if the user uses a different file, e.g. shakespeare_short.txt, it should indicate that filename instead. You should also test your code with the file shakespeare_short.txt. (Don’t forget that you will need to include the encoding parameter when you oepn the file.) You can find this file on the ‘Resources’ page of the course web site. When you run your code with this file, the last few lines should say: Line #374 has 1 words.Line #375 has 9 words.Line #376 has 6 words.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