Functions and Iterations and Strings. Oh my!

I thought about breaking up this post but decided not to.  The material here will probably take 2-3 weeks to get through so take your time and learn it well.  It isn't important to learn every nuance, but it is important to understand why certain things work the way they do.

Read chapter three, "Functions".
  • Do all five exercises in the chapter.
  • Explain in your own words why breaking a large program into smaller functions can be useful.
Read chapter six, "Fruitful Functions".
  • Also read 5.8-5.10 on recursion as this chapter expands on that and we skipped those sections earlier.
  • Do all 8 exercises in this section.  You might need some help understanding a few of the exercises, don't hesitate to ask.
Read chapter seven, "Iteration". 
  • Do the 5 exercises in this chapter.
  • Read about the Python keyword 'continue'.  Explain the difference between 'break' and 'continue'.
Read chapter eight, "Strings" 
  • Do the 12 exercises for this chapter as well
HANGMAN
Wow, that was a lot of reading and a lot of exercises.  Let's get to the fun stuff now and play Hangman!  Well, first we need to write the program.  This time I've started the program for you.   Download hangman.py and a text file I grabbed that has all the words used in the Book of Mormon.  If you want you can search for a dictionary file that has all the words in the English language, or create your own file of animal words or something.  The program is expecting a file that has one word per line.  I got the ascii graphics for the game from this site: https://inventwithpython.com/

Try running the file, it should print an empty hangman game. Since we haven't covered files and lists yet I wrote the function that opens a file and selects a random word.  I also started the function to display the game, we'll add to that in a bit.

First, let's get some input from the user.  Write a function that asks for input and then validates that input.  What do I mean by validate?  Well, when we ask for input we could get any thing from the person playing the game.  Since we don't have words with numbers or symbols in them we don't want to accept those.  What if the person types in more than one letter? If the user doesn't give us a valid guess, keep asking until they do.  Once they do enter a valid guess, return that string from your function.  Call the function and test it out with all sorts of input.  To make it easier to test you might want to start your main while loop for the game so we can enter input in without having to restart our program after each letter.  One other thing, look through your list of words we have, are there any upper case letters?  To a computer 'A' is different from 'a', make sure you take that into account when accepting guesses (in this case we can accept upper case letters, we just need to change them into lower case, fortunately there's a function for that built into Python).

After that you should draw out a block diagram for your program and then fill in the rest of the functionality.  We can work on that together and then break up the coding into small, testable pieces.  When you're done you should have a hangman game that the user can play over and over again until they say they don't want to any more.



Comments