To start this assignment, download this zip file.
Homework 4f — Random and coiteration
1. Quote Chooser
Write a program that displays a randomly selected quote from a file.
The name of the quotes file is provided as a commandline argument.
The quotes file has the following format:
- Each quote is on a separate line
- The quote is separated into parts by
|
characters
Example line
Life is infinitely stranger than anything which| the mind of man could invent.| - Arthur Conan Doyle
Your program should randomly select a quote from the provided file and then display the quote with each part on a separate line.
For example, if the above quote were selected from the quotes file, it should be displayed this way:
Life is infinitely stranger than anything which
the mind of man could invent.
- Arthur Conan Doyle
Testing
There is a provided test_quote_chooser.py
file that will grade your program, but it might not be very helpful in debugging your program.
To make sure your program is working correctly, you should run it on your own in the terminal.
Change to the directory containing quote_chooser.py
. You can run your program with the provided quotes file like this:
Windows
python quote_chooser.py test_files\quotes.txt
Mac
python quote_chooser.py test_files/quotes.txt
You should run your program several times to see that the quotes are being selected randomly (i.e. it shouldn’t be the same quote every time, and every quote should show up eventually).
2. Compare strings
Write a program named compare_strings.py
that prompts the user for two words.
Then compare the words character-by-character and print the result.
- If the pair of characters match, output a
*
- If the pair does not match, output a
.
The user enters an empty string for word 1 to end the program.
Follow is some example input and output. Follow this same format.
python compare_strings.py
Word 1: when
Word 2: what
**..
Word 1: tacos
Word 2: catch
.*...
Word 1:
Note
You will want to write you own main block.
def main():
# Write code here
if __name__ == '__main__':
main()
3. Word guess
Write a program named word_guess.py
that takes a file as an argument. This
file should contain a sequence of words, one word per line.
The program then randomly chooses a word from this file for the user to guess. The user guesses a word:
- if the guess matches the secret word, print “That’s it!”
- otherwise, if the guess is a substring of the secret word, print “almost”
- otherwise, if any of the letters in the guess can be found in the secret word, print “close”
- otherwise print “nope”
The file word_list.txt
contains an example file to use for input:
word_list.txt
cat
dog
fish
Following is an example of how you might run the program:
% python word_guess.py word_list.txt
Guess a word: dog
nope
Guess a word: ball
close
Guess a word: at
almost
Guess a word: cat
That's it!
Following is an example of how you might run the program:
% python word_guess.py word_list.txt
Guess a word: fish
nope
Guess a word: cat
nope
Guess a word: done
close
Guess a word: do
almost
Guess a word: dog
That's it!
Tips
When reading in lines from a file, each line will end with a newline character
\n
. You may find it useful to remove this character using strip. For example:
line = line.strip()
This will remove any whitespace from the start and end of line, including the newline character.
For the “close” option, you should define a function that takes two words, and returns True or False.
def any_char_matches(word1: str, word2: str) -> bool:
# Write code here
If word1 was ‘dog’, and word1 was ‘catastrophe’, you could:
- Check if ‘d’ is in catastrophe.
- Check if ‘o’ is in catastrophe.
- Since it is, return True.
- If no letters of ‘dog’ are in ‘catastrophe’, return False
Tests
Be sure you can pass the tests before you turn in the assignment. Review the guide on using pytest if you need to review using pytest and what to do if a test fails.
Grading
Activity | Points |
---|---|
Quote Chooser | 5 |
Compare Strings | 5 |
Word Guess | 10 |
Manual grading will focus on decomposition, fluency, and use of coiteration.