We have seen more examples of processing files using loops. We have also talked about errors and the debugger, and we started talking about exceptions. Next week, we will complete exceptions and then continue studying programming patterns (Chapter 5) more systematically: decision patterns (5.1), simple loops (5.2), two-dimensional lists and nested loop (5.3). After that we'll see a new type of loop: the while loop (5.4-5.6) and some new data structures, including dictionaries (Chapter 6).
Submission: The homework is due by midnight (I will not accept late homeworks). You can submit your homework through d2l into the drop-box for this homework. Please prepare your homework as a single file containing all answers (e.g. doc, docx, or pdf, not a zip file
).When submitting programs, include the programs, together with screenshots of test-runs of your program (make sure screenshots are sized so the text is legible; resize and/or crop the images).
1. (Reading Assignment) Reread Section 4.3, and read Section 4.4. If you want to read ahead, finish Section 5.2 and continue with Chapter 5 of the textbook
2. (Debugging, 10pt) Download and run the code in badacronym.py. If the code were working correctly, it should create results as shown below. But it doesn't work correctly. Debug this code using the built-in debugger, ad-hoc print-statements, or Pythontutor. For each error you find, briefly document how you found it, and how you fixed it. Note: don't just write correct code for solving the problem, that's easy. Instead, try to trace the errors made by the programmer of badacronym.py.
3. (Scores, 15pt) In class we saw how to extract a homework score of a particular student from scores.csv. For the current problem, you want to write a function class_score(lastname) which calculates the average score of the student with last name lastname. Do this in two steps.
Hint: process the file in lines (you can use infile.readlines() to read the file into a list of lines or read the whole file as a string and split it by '\n'). Each line consists of two parts: the currency shorthand and a number. You can get each part by splitting again (by the tab character, '\t'); note that that will give you two strings. Finally, make sure you do an example by hand first to make sure you use the conversion rate correctly.
5. (Extra Credit, 5pt) Extend the currency converter in problem 4 to curconv(curfrom, curto, amt) that converts the amount amt in currency curfrom to currency curto (by going through the dollar value).