In class we continued talking about control flow, seeing different types of loops; we then talked about functions (Section 3.3) and passing parameters (Section 3.5). We then talked about a new I/O mechanism: accessing files (Section 4.3). Next week, we will complete chapter 4, including more on strings and formatted output (Sections 4.1, 4.2), and see more examples of file processing (specifically, how to write/create them, Section 4.3). We'll then talk about exceptions (Section 4.4) and move on to more sophisticated control flow patterns (Chapter 5). If you're working with text-files on a MAC, be sure to include
at the top of your program, and then instead of open use codecs.open:
infile = codecs.open(name,'r','UTF-8')
That should work.
Submission: The homework is due by midnight (I will not accept late homeworks). You can submit your homework through d2l.
1. (Reading Assignment) (Re)read Section 3.2, and read Sections 3.3, 3.5, and Sections 4.1-4.3 of the textbook (some of the material in 4.1/4.2 we'll see next time). If you want to read ahead, read 4.4 and start on chapter 5.
2. (Ifs and loops, 15pt) Write code for the following tasks. Include screenshots of testruns.
[5pt] Write a for loop that lists all the composers on a list, say
['Antheil', 'Saint-Saens', 'Price', 'Easdale', 'Nielsen'], whose
last letter is the same as the first letter. (so Saint-Saens, Easdale
and Nielsen should get listed, but not the others.)
Of course the code should work for arbitrary lists, not just this particular list. Hint: the trickiest part may be the condition on the last name. Research the .lower() method of a string. You don't have to write a function for this problem.
[10pt] Prompt the user for two numbers (a lower and an upper bound), and
then print the powers of 2 from 2**a to 2**b.
Here is a sample runs
Hint: using format function makes output simpler. As for a) you don't need a function here.
3. (Functions, 10pt) Write a function password_check(oldpassword, newpassword) that has two inputs, newpassword and oldpassword and that accepts the new password (returns True) if the newpassword fulfills all of the following conditions:
newpassword is different from oldpassword
newpassword is at least 8 letters long
newpassword contains some character which is not a letter (i.e. a number, or some special character). Hint: research the string type using help(str) to look for appropriate tools to do this.
If the new password fails the check, your function should return False.
4. (File Reading, 15pt) You have friend who likes playing crossword puzzles and word games, and every so often she'll ask you: I need a word that starts with 'inn', 11 letters? You're ready to help her now. Only question is, where do you get the word list from? In the absence of a better source, you just use one of the textfiles you have lying around, e.g. innocents.txt, which we used in class, and which contains many words. So you want to implement a function wordhelp(start, length, fname) that looks through the file with name fname and lists all words with prefix start and of length length. Below are a couple of testruns:
Note that the program isn't perfect: punctuation counts as letters, and it'll list the same word repeatedly (every time it finds it). Both of those issues are acceptable for the purposes of this homework. Hint: this connects a couple of pieces we've seen, however you probably want to start with one of the occurs functions we implemented in week3g.py.
5. (Extra Credit: A Word Game, 10pt) With the new tools you have for reading files you want to implement a simple word game. Based on some writer's novel (like in the class examples, I'll use Mark Twain's Innocents abroad, available as a textfile innocents.txt, but feel free to use any other text you want to play with) you want to figure out which of two words they are more likely to use. So here is how the game proceeds: you are presented with two (randomly chosen) words from the novel, and you get to pick which one you think the writer is likely to have chosen more often. The program then tells you whether you were right and gives you the actual counts of both words. Write a function whichword() that implements this word game. Below are some test-runs with innocents.txt.
Hint: there are a couple of obstacles here, pick your fights, and fight them one at a time. How to get the words from the text? (We saw how to do that.) How to pick a random word? (Research the choice() method in the random module.) Initially, simply hardcode two particular words for testing. How to count how often each word occurs (use count method). How to prompt the user (use format). How to decide whether the user won? Write a logical condition.