BYU logo Computer Science

To start this assignment, download this zip file.

The following guide pages cover material needed for this assignment:

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


There is a provided 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 You can run your program with the provided quotes file like this:


python test_files\quotes.txt


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

Word 1: when
Word 2: what
Word 1: tacos
Word 2: catch
Word 1:


You will want to write you own main block.

def main():
	# Write code here

if __name__ == '__main__':

3. Word guess

Write a program named 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:



Following is an example of how you might run the program:

% python word_list.txt
Guess a word: dog
Guess a word: ball
Guess a word: at
Guess a word: cat
That's it!

Following is an example of how you might run the program:

% python word_list.txt
Guess a word: fish
Guess a word: cat
Guess a word: done
Guess a word: do
Guess a word: dog
That's it!


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


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.


Quote Chooser5
Compare Strings5
Word Guess10

Manual Grading

Refer to the Quality Code guide page for detailed explanations and examples on each of these rubric criteria.


The intent of this assignment is that you use zip for coiteration of lists and strings, not indexing. You shouldn’t convert strings into lists of characters before zipping them.

As a reminder, nested loops usually indicate a great place to further decompose your code. We often recommend moving the inside for loop to another function as a way to increase the quality of your code.

Remember, joining a list is not the only way to build a string. If you don’t have a delimiter, it makes sense to use += instead of appending to a list.