Homework 5 (due 5/14)
CSC 241

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.

a) [6pt] Write a function intlst(lst) which turns a list of strings (representing numbers) into a list of numbers.

b) [9pt] Write the function class_score(lastname) decribed above. Use the function intlst you wrote in a), it'll come in handy when processing the scores in each row.

4. (Currency Converter, 15pt) Write a function curconv(cur, amt) that as an input takes a currency (cur, e.g. 'USD', or 'EUR') and an amount (e.g. 100) and converts that amount into its equivalent value in US dollars ('USD'), returning the amount as a formatted string. The currency rates we have available are stored in a file currencies.txt. It shows how much $1 is worth in the foreign currency. E.g. in that file AUD (Australian dollars) is listed with a rate of 1.43, which means that $1 US is worth $1.43 AUD. The rate for Bitcoin (XBT) is 0.00017, so $1 is worth 0.00017 Bitcoins.

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).

Marcus Schaefer
Last updated: May 8th. 2019.