Skip to content

Instantly share code, notes, and snippets.

@sarguido
Last active January 4, 2016 13:29
Show Gist options
  • Save sarguido/8627743 to your computer and use it in GitHub Desktop.
Save sarguido/8627743 to your computer and use it in GitHub Desktop.
A2 PyLadies Intro to Python part 3
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Intro to Python - Part 3\n",
"\n",
"We're now ready to write a hangman program. We'll write it together in class, but this page will describe the different components of the program and why they're important. You can get the whole script from the Github repository.\n",
"\n",
"So, let's break it down. First we import the modules that we need: time and random. Time causes the delay at the beginning between the lines of text, and random selects a random word from our list of words for the game."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import time\n",
"import random "
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The next section is our welcome text. We use time.sleep() to slow down the pacing for the game player."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print \"Hello! It's time to play hangman!\"\n",
"time.sleep(1) \n",
"\n",
"print \"All of the items in this list have to do with programming in Python. Have fun!\"\n",
"time.sleep(1)\n",
"\n",
"print \"Start guessing...\"\n",
"time.sleep(0.5)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Hello! It's time to play hangman!\n",
"All of the items in this list have to do with programming in Python. Have fun!"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Start guessing..."
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's create our wordlist:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"wordlist = [\"python\", \"programming\", \"pyladies\", \"strings\", \"variables\", \"input\", \n",
" \"list\", \"loop\", \"module\", \"developer\"]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We're going to use a while loop to run the game. We want to stop our while loop when we're tired of playing, so we need to create a variable that we can change later in order to stop the game. Let's call it \"keep going\", and we're going to set its value to an empty string, since we don't know if we want to keep going yet."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"keep_going = \"\""
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And now, let's get into the meat of the program. We'll initialize our while loop, so that what we're saying is that we'll keep going as long as keep_going is not equal to \"no\"."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"while keep_going != \"no\":"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Inside of our loop, we select a random word from our wordlist."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" word = random.choice(wordlist)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We set the number of turns we have to 10. This means that if we don't guess the word in 10 tries, we lose!"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" turns = 10"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We'll also create a variable called \"guesses\". This is where we'll store all of our letter guesses."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" guesses = \"\""
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Inside of our while loop, we're going to create another while loop! This is to keep track of how many times we've guessed. We're going to tell our program to keep going as long as we still have guesses left (we're going to start with 10 guesses and subtract one every time we guess incorrectly."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" while turns > 0:"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The next thing we need to keep track of is if we've guessed all of the letters in the word. If we've correctly guessed the word, we want to make sure we end the program. We'll keep track of that in a variable called \"not_guessed\", which will keep track of the letters we haven't guessed yet."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" not_guessed = 0"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we want to look at the characters in our secret word. If we've guessed any of those characters correctly, we want to print out those correct guesses. Writing \"print char,\" will print out the letter followed by a space."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" for char in word:\n",
" if char in guesses:\n",
" print char,"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For those characters we haven't guessed, we're going to print out an underscore followed by a space, and we're going to add 1 to our \"not_guessed\" variable."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" else:\n",
" print \"_\", \n",
" not_guessed = not_guessed + 1"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we're going to check to see if we've won. If we've correctly guessed all of the letters, \"not_guessed\" will be equal to 0. If that's true, we'll print out that you've won!"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" if not_guessed == 0: \n",
" print \"You won! Good job!!\""
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we're going to ask the player if they want to keep playing. That's where our \"keep_going\" variable comes into play. If we want to end the game, all we have to do is type \"no\", and then our outer while loop will no longer happen."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" keep_going = raw_input(\"Would you like to play again? \")"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The final thing we do in this conditional block is break out of the inner while loop. If the player has won the game, there's no need to keep on guessing. So, there's no need to continue with the current word. If the player wants to keep playing, this break sets the player back to the outer loop, which selects another word for guessing."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" break"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here's the first text the player will see that asks for a guess. We'll store that in a variable named guess, and we'll also store it in our guesses variable, in order to keep track of whether we've won or not."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" guess = raw_input(\"Guess a character: \") \n",
" guesses = guesses + guess "
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The next step is to check if the guess is correct. If it's not, we'll subtract a turn, print out that the guess is wrong, and tell the player how many turns are left."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" if guess not in word: \n",
" turns = turns - 1 \n",
" print \"Wrong.\" \n",
" print \"You have\", turns, \"more guesses.\""
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The final block to this program is the \"You lose\" block. If you've guess incorrectly so many times that turns now equals 0, we will print out that you've lost, we'll tell the player what the word was, and then ask the player if they want to play again. If the answer is yes, we'll exit the inner while loop because turns is no longer greater than 0 and select a new word for guessing. If the answer is no, we'll exit the outer loop and thus the entire program."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
" if turns == 0: \n",
" print \"You lose. The word was\", word\n",
" keep_going = raw_input(\"Would you like to play again? \")"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And that's hangman!"
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment