Download Lesson 13: Hangman I - Introduction

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
IEC 2015 - Python Programming
Week 13 - Hangman I
Hangman Introduction
Today, we are going to make the basic building block of a hangman program. Before we
start, let’s think about hangman the way a computer would. What steps do you need to go
through?
1.
2.
3.
4.
Select a word
Wait for someone to guess a letter
Check if the word we selected contains that letter
If it does, we fill in the appropriate blanks. If it doesn’t, we add an arm/leg to the
hangman
5. Go back to step 2.
Step 1: selecting a word
To make it easy for us, we will select a word for the computer.
SecretWord = “apples”
You could use any word you like, fruit, candy, cars, ANYTHING at all!
So that’s step 1 of our hangman program finished! (Look below for extra credit, where
the program picks its own word from the list you provided)
Step 2: wait for a guess. We’ve done this plenty of times:
print(“Please guess a letter”)
guess = input()
Step 3: check if our secret word contains the letter they guessed. For this, we need to use
something new, but it should look familiar. If we want to see if a certain letter is inside a
word, we use in. We use it the same way we use >, <, or = = to compare numbers. Just
stick it in an if statement:
if guess in SecretWord:
print(“There are one or more ” + guess + “-s in the
secret word”)
else:
print('There are no ' + guess + '-s in the secret
word')
1
IEC 2015 - Python Programming
Week 13 - Hangman I
That’s step 3 done. Step 4 takes some work, and we’ll get to it another time.
Step 5 should look familiar. it’s a loop! In Hangman, you keep going through the loop
(that is, you keep going back to step 2) until you guess all the letters or the hangman
drawing is finished (which takes 5 wrong guesses). For now, we’ll just make a simple
loop that keeps going until you want to stop guessing letters. This is a useful trick that
can show up in lots of programs. Pick the part of your program that you want to
repeat, and start your loop like this:
GuessAgain = “y”
while GuessAgain == “y”:
Remember, you’ll need to indent (move a little bit to the right) everything inside the
loop. At the end of the piece of code you want to repeat, type this:
print(“Do you want to guess again? (y/n)”)
GuessAgain = input()
Because we started the loop with while GuessAgain = “y”, whether or not we repeat the
loop is determined by what the person at the keyboard types. If they don’t type “y”, we
stop!
--------------------------------------------------------SecretWord = 'apples'
GuessAgain = 'y'
while GuessAgain == 'y':
print('Please guess a letter')
guess = input()
if guess in SecretWord:
print('There are one or more ' + guess + ' -s in
the secret word')
else:
print('There are no ' + guess + ' -s in the secret
word')
print('Do you want to guess again? (y/n)')
GuessAgain = input()
--------------------------------------------------------Extra Credit:
2
IEC 2015 - Python Programming
Week 13 - Hangman I
Making the program choose the word itself, using lists and random numbers:
i) For step 1, instead of giving ‘one’ word that you have to change each time you run,
make a ‘LIST’ of 5 words.
ii) Using ‘random’, pick a number. Do you remember this from previous classes?
The start of your program should have:
import random
And then, the program picks a number on its own like this:
num = random.randint(1,5)
You have a list from (i) and a number from (ii), how would you pick the word? Write
your program and have Mrs. Levine play the game for you.
--------------------------------------------------------import random
wordList = {
1: 'carpet',
2: 'basketball',
3: 'bird',
4: 'tree',
5: 'science'
}
num = random.randint(1,5)
SecretWord = wordList[num]
GuessAgain = 'y'
while GuessAgain == 'y':
print('Please guess a letter')
guess = input()\
if guess in SecretWord:
print('There are one or more ' + guess + ' -s in
the secret word')
else:
print('There are no ' + guess + ' -s in the secret
word')
3
IEC 2015 - Python Programming
Week 13 - Hangman I
print('Do you want to guess again? (y/n)')
GuessAgain = input()
print('The word was ' +SecretWord)
4