.py
files containing your work. (If you upload a screenshot or other file format, you won't get credit.)This homework assignment must be submitted in Gradescope by 10am CST on Tuesday, October 19, 2021.
This homework focuses on exceptions, the os module, and the function- and assignment-related concepts from Lecture 20 (multiple return values, tuples, tuple assignment, variadic functions, iterable unpacking).
Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.
The course materials you may refer to for this homework are:
This homework assignment has 2 problems, numbered 2 and 3. The grading breakdown is:
Points | Item |
---|---|
2 | Autograder |
4 | Problem 2 |
4 | Problem 3 |
10 | Total |
Ask your instructor or TA a question by email, in office hours, or on discord.
Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.
In Homework 7, problem 3 (titled "Chemical element CSV to JSON"), you wrote a program to convert a CSV file containing data about chemical elements to a JSON file. The program used hard-coded filenames elements.csv
and elements.json
.
Modify this program (either start with one of the solutions posted to the course web site, or your own solution) so that it is robust to all of the following conditions:
elements.csv
does not existelements.csv
exists and is a CSV file, but it has fewer than 3 columnselements.csv
exists and is a CSV file with columns called number
, abbreviation
, and name
, but some of the values in the column number
are not integersIf any of these conditions arises, the program should print an error message describing the situation and then exit without creating elements.json
nor writing anything to it.
Hint: You can detect these error conditions because they cause exceptions. I recommend forcing each of these things to happen and checking the type of exception that results.
Save the result as hwk8prob2.py
.
Most of the sample programs we've written in lecture contain a comment listing the lecture or lectures in which we worked on or discussed the program. Usually such a comment looks like this:
# Lecture 12
or this:
# MCS 260 Fall 2021 Lecture 9
This problem is motivated by the question: How could you write a Python script to find all such example programs that mention a specific lecture?
Write a program that prints a list of names of all files in the current directory that meet both of the following criteria:
.py
.Lecture
somewhere.To test this program, you should probably create another file in the current working directory that ends with .py
and which contains the string Lecture
somewhere!
You don't need to split a file into words for this problem; for example, if a file has first line asdfLecturefghil
and its name ends in .py
, this program should print its name.
Save the result as hwk8prob3.py
.
Restricted methods note: For full credit your answer may only import these modules: os
, json
, and csv
.
BE CAREFUL: This program should only open files for reading. It would be dangerous to open all files in the current directory for writing, because that would delete the current contents!
Example: When I run a program meeting the specifications of this problem with the current working directory set to the samplecode directory for MCS 260, the output is:
quadroots.py
wordlist.py
jsonreaddemo.py
wordstats3file.py
terminal.py
terminal_2pm.py
inputyesno.py
terminal2.py
collatz.py
csvwritedemo.py
rectangle.py
parentheses.py
csvdictreaddemo.py
greet.py
csvinfo.py
spillreport.py
quadsquare.py
csvreaddemo.py
wordstats3.py
terminal_10am.py
charactertype.py
parentheses2.py
collatz_nofunction.py