Old version
This is the CS 111 site as it appeared on May 10, 2018.
Problem Set 8 FAQ
If you don’t see your question here, post it on Piazza or come to office hours! See the links in the navigation bar for both of those options.
General questions
-
I keep getting a
NoneTypeerror. Why does this happen?If your dates are suddenly becoming
Noneand you do not know why, check over your code to make sure that you are not assigning the result of thetomorrowmethod to a variable.x = d.tomorrow() # incorrect self = self.tomorrow() # incorrect
Remember,
tomorrowdoes not return a value. We call it to modify an object, and we do not want it to return anything. As a result, you should not assign the result of a call totomorrowto a variable. If you do, that variable will end up with a value ofNone(a special value that is returned when a function does not explicitly return a value), and that can then lead to aNoneTypeerror.Instead, all of your calls to
tomorrowshould occur all by themselves on their own lines:d.tomorrow() # correct self.tomorrow() # correct
This same rule also applies to calls to
add_n_days, since it should not return a value either. -
How do I read from a file?
In lecture, we presented two different ways to read the contents of a file. You can consult the lecture notes from last week, or check out the example code online.
In those examples, you will see that you can process a file line by line, or read the entire file as one big string.
Problem 2
See also question 1 from the general questions above.
-
The logic of my
diffmethod looks right, but it’s giving the wrong answer. Do you have any suggestions?The
diffmethods should use other methods from yourDateclass. If your logic indiffseems correct but you are still getting the wrong answer, you should check your methods from earlier in the problem to see if they are correct. Even if you pass the test cases we have given you for those other methods, there may still be a bug. For instance, tryis_beforewith two differentDateobjects from the same year and month. Does the result seem right? What about for twoDates in different months? -
My
diffmethod hangs (i.e., it keeps running indefinitely without giving me a return value) when I call it for certain dates. What could the problem be?The problem could be in your
tomorrowmethod. Make sure that it actually advances the date in all possible cases. In particular, if you are using anif-elifstatement, you may want to add anelsecase to ensure that the date is advanced no matter what. Iftomorrowfails to advance the date in certain cases, then yourdiffmethod can end up in an infinite loop, which causes the method to hang.Another thing worth checking is whether your
diffmethod may be incorrectly advancing its copy of the later of the two dates, rather than its copy of the earlier of the two dates. This can happen if youris_beforemethod is not returning the correct value when it compares the two dates in question. -
Is it good enough if my
Datemethods pass the test cases given in the assignment?Even if your
Datemethods pass the test cases given in the assignment, they may still be incorrect. We highly recommend thinking of your own test cases to ensure that yourDatemethods work properly. If you find a case in which a method gives invalid results, use temporaryprintstatements to figure out where the unexpected behavior occurs.
Problem 3
-
How do I start Problem 3?
If problem 3 seems unclear, read over the relevant lecture slides. We have already given you a partial implementation of the
create_dictionaryfunction in those notes. The first thing you need to do is add code that will read all of the file’s text as a single string, and then split it into a list of words. You may want to consult one of the other functions from lecture to see how to do this.Then use the rest of the code skeleton from the lecture notes, adding whatever code is needed to that skeleton.
The lecture notes also include suggestions for the
generate_textfunction.