Week 4 – 1/31/15 – Hangman Part 1

Hang Man
We are going to code up the game Hangman  in Python. Once we do this we can then use Minecraft as a graphical interface for the game. This should be a lot of fun.

In order to do this we are going to be manipulating Strings. I am going to give you simple excercises that will help you code up the game. As always write a few lines of code and test.

A:   Lets assume that the mystery_word is “minecraft” so we will want to display  “ _ _ _ _ _ _ _ _”

Type in the following in IDLE

x= “minecraft”
len(x)

What did this return?

y = “ab”
z = y + y
z

What did this return?

Now can you write a script where you set x equal to a word and we get the correct display?

For example:

x=“hello”  creates   “ _ _ _ _ _”

We might want to use this output again so how do you store it? What sort of variable do we use?

B:  Lets assume that the mystery_word is “chicken” and then the user is asked to guess a letter. Assume the user guesses “c”.  We then need to know that the the 1st and 4th letters have a c.

Python has many tools to help you with Strings. So there are many ways to do things and feel free to get creative.

Type in:

x= “minecraft”
x.find(“c”)

What did this return?

Now type:

y = “hello”
y.find(“l”)

What did this return?

Now type:

temp = y[0]
temp
temp y[1]
temp

Now write a script that takes a word and prints out a letter on each line so:

x = “summer”

prints out:

s
u
m
m
e
r

Now lets go back a bit:

y = “hello”
temp = y[0]
temp.find(“l”)

What printed out?

temp = y[1]
temp.find(“l”)
what printed out?
temp = y[2]
temp.find(“l”)

What printed out?

When we are repeating lines of code what is a good idea?

Now try to write a script that finds where a letter appears in a word (remember first position is 0)

So if:

x = “hello”
guess = “l”

we want to get 2,3.

Feel free to email me with questions or post them right here.