Assignment - DePaul University



Assignment Seven – Winter quarter, 2020?SubmissionInclude your full name as a comment in the first line of your Python program. Also indicate the name of any other student with whom you worked on your assignment. notes / suggestionsThis assignment may seem long, but it's mainly because I am trying to be as specific as possible about the requirements. I also give several examples, hints, suggestions, tips, etc in this document. As always: Start early – this means begin the week's work by doing a LOT of practicing the code concepts and techniques from the lectures. As before , please be sure and use the exact function identifiers that I specify in each problem. Please remove all test code before submitting your assignments.? This includes the sample runs I provide at the bototm of this page. I will paste those in myself when I am grading your assignments. TIPS:????????? Remember to start by getting comfortable with the materials including dictionaries and while loops, etc. Do NOT try to learn the material by doing the homework! That is a slow, stressful, and highly ineffective way of learning the material!????????? Remember to start with pseudocode. ????????? As always, remember to start small and build. For example, do NOT feel that you have to do things in the order in which they are specified in the assignment. E.g. If you want, you can do the try/except stuff once your are done with the other things. In problem #2, I would suggest that you should only worry about the infinite loop after you have gotten the other functionality taken care of. ????????? Try not to get overwhelmed. The key is to 1)practice the concepts (dictionaries, loops, try/except), 2)pseudocode, and then 3)do small pieces at a time. Problems?Problem #1: Open the file students.csv inside a text editor such as Notepad or TextEdit. This is a text file containing a list of students registred for a graduate course in chemistry. NOTE: On every line, the items are separated by commas. What this means is that using split() in the "usual" way will not work. Recall that split() splits based on whitespace. However in this example, we must split on commas instead. Hint: There is a parameter in the split() function that allows you to tell the function to split on something besides whitespace. ?Also important: Note that there is a \n at the end of every line. At some oint in your program, when working with each line, you will need to remove this character. ?Write a function called createStudentDict()that opens this file and populates a dictionary with all of the students in the file. The key to teach item in the dictionary should be the student ID which is present in the first column. This student ID key should be recorded as a string. The value should be a list in which the first item is the student's name (should be stored as a string), the second item is the student's age (integer), and the third item is the student's current full-time occupation (string).? For example, the first line in the file is:? 7373,Walter White,52,Teacher??Your dictionary should have an entry that looks something like this:'7373':['Walter White',52,'Teacher']Your function should return the complete dictionary. Therefore, invoking the function like so:>>> print( createStudentDict() )?Should generate the following output:{'7373': ['Walter White', 52, 'Teacher'], '8274': ['Skyler White', 49, 'Author'], '9651': ['Jesse Pinkman', 27, 'Student'], '2213': ['Saul Goodman', 43, 'Lawyer'], '6666': ['Gus Fring', 54, 'Chicken Guy'], '8787': ['Kim Wexler', 36, 'Lawyer'], '9999': ['Tuco Salamanca', 53, 'Drug Lord']}??Requirements:Place the code to open the file inside a try/except block. Catch a FileNotFoundError. Your handling code can simply print a message saying there was a problem opening the file. Note: Do not place all of your code in this try block! Only the relevant code should be there, i.e. the code that opens the file, reads in info and then closes the file. The rest of your code should not be inside this try block.Note that the age is an integer, so make sure that this value is indeed stored as an integer in your dictionary.You will note that there is a newline character at the end of every line in the CSV file. Remove this \n character (i.e. replace with an empty string). This is not hard to do. As is so often the case, invest a few minutes thinking about the best approach to doing so. Once you happen on a solution, you'll see that the code is not compicated. ?PSEUDOCODE????????? For this problem, in a block comment at the end of your file, provide your pseudocode for the problem. ????????? DO NOT NOT NOT simply go back to your code after doing it, and then translate it into pseudocode. That is precisely the opposite of the intended purpose of pseudocode! Your pseudocode MUST come first! I should be able to look at your pseudocode and recognize that you really thought out and planned a strategy BEFORE embarking on your code. ????????? If, after doing the pseudocode, and starting on yor Python code, you realize that something in your pseudocode isn't correct or optimal, it IS perfecty permissible to go back and update your pseudocode. But the whole point of pseudocode is to get you to really think through the strategy of how to attack a programming problem. ????????? My own pseudocode typically starts looking almost 100% like English and nothing like Python. I might use the word 'loop', or 'if' in there, but that's about the extent of it.? I often have a "first version" that is almost purely English, and a "second version" that looks about 2/3 like English and about 1/3 like Python. ?Problem #2:Create a function called searchStudentDictionary() that accepts the dictionary that you created in the prervious problem as an argument. ?NOTE: If you were unable to get the previous problem working (though I hope this won't be the case for many/any of you!) you can copy / paste the code below in order to create the dictionary:?dctStudents = {'7373':['Walter White',52,'Teacher'],'8274':['Skyler White',49,'Author'],'9651':['Jesse Pinkman',27,'Student'],'2213':['Saul Goodman',43,'Lawyer'],'6666':['Gus Fring',54,'Chicken Guy'],'8787':['Kim Wexler',36,'Lawyer'],'9999':['Tuco Salamanca',53,'Drug Lord'] }??When you invoke the function, it should begin by printint out all of the keys in the dictionary like so:? 7373 8274? 9651? 2213? 6666? 8787? 9999?Note that if, in your function you simply say:? ? print( dictionary.keys() )?you'll get something ugly like this:? dict_keys(['7373', '8274', '9651', '2213', '6666', '8787', '9999'])?Instead, your output should separate each key from the next with a tab like so:7373? 8274? 9651? 2213? 6666? 8787? 9999Hint: Recall that the character to print a tab is \t.?Hint: Recall that you can cycle through all keys of a dictionary with:? for k in dictionary.keys():?Then prompt the user to enter a key:?Here is a list of keys in your dictionary:7373? 8274? 9651? 2213? 6666? 8787? 9999? ?Choose a key:?When the user enters a key, your function should print the values of that key. ?Choose a key: 9651?Name: Jesse PinkmanAge: 27Occupation: Student?Note that the items in the list should be printed in a nice format as shown above (use a format string). ?Hint: Recall that the value of each key is a list. So for the name you can print out the first item in the list, for the age, the second item, and for the occupation, the third item.?The part where you ask the user to enter a key and where you retrieve the value for that key should be placed inside a try block. This block should catch a KeyError – which happens if you try to retrieve a dictionary value using a key that does not exist in that dictionary. Inside your except block, your program should output a message such as:? ?That student is not in the class.?Finally (literally – do this part last!!) wrap the entire code in which you ask the user to enter a key (including the try/catch) block inside a 'while True' loop. As you know, this is an infinite loop. In other words, the loop will keep prompting the suer to enter a key until they enter one that is actually present inside the dictionary. Once they enter a valid key, you should output the information, and the function should end. (Recall that you can use 'return' to end a function).TESTINGYour assignment will be tested using the following code. Feel free to use this code to test your assignment. You can see the expected output below. ?Code to test problem #1:>>> dctStudents = createStudentDict()>>> print(dctStudents)?The previous lines should output{'7373': ['Walter White', 52, 'Teacher'], '8274': ['Skyler White', 49, 'Author'], '9651': ['Jesse Pinkman', 27, 'Student'], '2213': ['Saul Goodman', 43, 'Lawyer'], '6666': ['Gus Fring', 54, 'Chicken Guy'], '8787': ['Kim Wexler', 36, 'Lawyer'], '9999': ['Tuco Salamanca', 53, 'Drug Lord']}?Code to test problem #2:>>> searchStudentDictionary(dctStudents)?The previous line should result in something like this:Here is a list of keys in your dictionary:7373? 8274? 9651? 2213? 6666? 8787? 9999? ?Choose a key: 99999That student is not in the class.Please choose from the following: 7373? 8274? 9651? 2213? 6666? 8787? 9999? ?Choose a key: 88888That student is not in the class.Please choose from the following: 7373? 8274? 9651? 2213? 6666? 8787? 9999? ?Choose a key: 7373Name: Walter WhiteAge: 52Occupation: Teacher?If you have any questions regarding this assignment, please post them to the Discussion Forum.PLEASE DO NOT POST YOUR OWN CODE THOUGH! ................
................

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

Google Online Preview   Download