Programming Assignment 6 (Hangman)
Write a Python program to implement the "Hangman" game of guessing phrases. A sample session is shown here.
This program will use the following Python Class (PhraseBank.py) to initialize a phrase bank from a file provided in sys.argv[1], retrieve the next phrase, get and set topic, as well as retrieve the list of all topics.
A sample file containing topics and phrases within each topic is available at phrases.txt. This file will contain a topic line that begins with 2 "*"s followed by a topic. The lines that follow will be the phrases (one per line) under that topic. This pattern may be repeated for several topics.
import random class PhraseBank: def __init__(self,fname): pass def next_phrase(self,topic): pass def get_all_topics(self): pass
Some Tips
- In the main program (Hangman.py), you should use a variable:
remaining_letters = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z "
which will keep track of the remaining letters of the alphabet that have not been used by the user. - It will be useful to devise the following functions to compartmentalize the code:
play_one_round: plays one round of the game replace_by_stars: takes target phrase and replaces all remaining letters by * test_if_win_or_loss: checks to see if win or loss and prints appropriate message get_valid_guess: repeatedly prompts user for their guess until they provide one of the remaining letters