You may work with one partner on this assignment. If you work with a partner, put your name and the name of your partner in the list of authors at the top of your program. Only one partner needs to run handin21 to submit the files for the group. Both partners will receive the same grade.
Run update21, if you haven't already, to create the cs21/labs/07 directory. Then cd into your cs21/labs/07 directory and create the python programs for lab 7 in this directory (handin21 looks for your lab 7 assignments in your cs21/labs/07 directory). If you just want to start with your Lab 6 solution:
$ cd ~/cs21/labs/07 $ cp ../06/guess.py hangman.py
For this assignment, you will write a program that implements the hangman game (you can use the "apples falling from the tree" alternative if you want). The focus of this assignment is for you to practice using top-down design to construct your solution incrementally. You should make use of your programs from Lab 6 in this assignment.
Hangman is a simple letter guessing game. The computer picks a word (from a file) and displays it with underscores replacing the letters. The user tries to figure out the word by guessing one letter at a time. The user loses if they guess too many incorrect letters.
Here are two examples of how your hangman program should work.
$ python hangman.py ------------------------------- Welcome to H A N G M A N v1.0 ------------------------------- _____ incorrect guesses left: 6 Enter a letter: e Sorry...no e... _____ incorrect guesses left: 5 Enter a letter: a yes, there's a a in the mystery word(s)! _a___ incorrect guesses left: 5 Enter a letter: p yes, there's a p in the mystery word(s)! _app_ incorrect guesses left: 5 Enter a letter: a you already guessed that letter :( Enter a letter: h yes, there's a h in the mystery word(s)! happ_ incorrect guesses left: 5 Enter a letter: y yes, there's a y in the mystery word(s)! Correct....HAPPY You got it with 5 guesses left! SUPER.
$ python hangman.py ------------------------------- Welcome to H A N G M A N v1.0 ------------------------------- ________ incorrect guesses left: 6 Enter a letter: q Sorry...no q... ________ incorrect guesses left: 5 Enter a letter: w Sorry...no w... ________ incorrect guesses left: 4 Enter a letter: d Sorry...no d... ________ incorrect guesses left: 3 Enter a letter: z Sorry...no z... ________ incorrect guesses left: 2 Enter a letter: x Sorry...no x... ________ incorrect guesses left: 1 Enter a letter: m Sorry...no m... You L O S E !!! The answer was....PLATYPUS
if 'a' in mystring: ...Use not in to test if a character is not in a string.
>>> from string import * >>> word = "apple" >>> charList = list(word) >>> charList ['a', 'p', 'p', 'l', 'e'] >>> rejoined = join(charList, '') >>> rejoined 'apple'or, using the str class methods:
>>> rejoined = ''.join(charList) >>> rejoined 'apple'
Here are some fun extensions that you might want to add to your program:
Once you are satisfied with your program, hand it in by typing handin21 at the unix prompt. If you work with a partner on this assignment, only one of you needs to run handin21.