34358-05 Hangman
34358-05 Hangman
3 Hangman
Keep track of your progress by
ticking off the boxes below:
Introduction
Let’s build a game: Hangman! The computer will pick a word, and the player can guess
it letter-by-letter, but if they make too many wrong guesses, they’ll lose.
Activity Checklist
1. Open IDLE, and open a new window.
2. Write in the following code:
from random import choice
word = choice([“code”, “club”])
print(word)
3. Save your program, and run it. What word does it print?
4. Run it again, does it print a different word?
Each time you run this program, it picks a random word from the list [“code”,
“club”], using the choice function.
Activity Checklist
1. With the same file, edit the code so it looks like this
from random import choice
word = choice([“code”, “club”])
out = “”
1
These projects are for use inside the UK only. All Code Clubs must be registered. You can check registered clubs on the map at www.codeclub.org.uk.
This coursework is developed in the open on GitHub, at www.github.com/CodeClub/. Come and join us!
Level
Hangman
3
Keep track of your progress by
ticking off the boxes below:
We use a new function input() to find out what the player typed. We use if to find
out if the letter was in the word.
We’ve got the essentials down, so let’s continue onward.
(Python 2 Note: Use raw_input if you’re on an old version of python)
Activity Checklist
1. In the same file, edit the code to look like this:
2
These projects are for use inside the UK only. All Code Clubs must be registered. You can check registered clubs on the map at www.codeclub.org.uk.
This coursework is developed in the open on GitHub, at www.github.com/CodeClub/. Come and join us!
Level
Hangman
3
Keep track of your progress by
ticking off the boxes below:
3
These projects are for use inside the UK only. All Code Clubs must be registered. You can check registered clubs on the map at www.codeclub.org.uk.
This coursework is developed in the open on GitHub, at www.github.com/CodeClub/. Come and join us!
Level
Hangman
3
Keep track of your progress by
ticking off the boxes below:
Activity Checklist
1. Edit the existing file, and change it to look like the following:
from random import choice
word = choice([“code”, “club”])
guessed = []
wrong = []
while True:
out = “”
for letter in word:
if letter in guessed:
out = out + letter
else:
out = out + “_”
if out == word:
print(“You guessed”, word)
break
print(“Guess the word:”, out)
guess = input()
if guess in guessed or guess in wrong:
print(“Already guessed”, guess)
elif guess in word:
print(“Yay”)
guessed.append(guess)
else:
print(“Nope”)
wrong.append(guess)
print()
We’re using a new list, wrong, to store all the guesses that weren’t right
Only one last thing before the game is complete, which is to only have a few chances
to guess.
4
These projects are for use inside the UK only. All Code Clubs must be registered. You can check registered clubs on the map at www.codeclub.org.uk.
This coursework is developed in the open on GitHub, at www.github.com/CodeClub/. Come and join us!
Level
Hangman
3
Keep track of your progress by
ticking off the boxes below:
Activity Checklist
1. Edit the file, to introduce a new variable, tries:
from random import choice
word = choice([“code”, “club”])
guessed = []
wrong = []
tries = 7
while tries > 0:
out = “”
for letter in word:
if letter in guessed:
out = out + letter
else:
out = out + “_”
if out == word:
break
print(“Guess the word:”, out)
print(tries, “chances left”)
guess = input()
if guess in guessed or guess in wrong:
print(“Already guessed”, guess)
elif guess in word:
print(“Yay”)
guessed.append(guess)
else:
print(“Nope”)
tries = tries - 1
wrong.append(guess)
print()
if tries:
print(“You guessed”, word)
else:
print(“You didn’t get”, word)
2. Run the file, and see what happens when you guess wrong letters
5
These projects are for use inside the UK only. All Code Clubs must be registered. You can check registered clubs on the map at www.codeclub.org.uk.
This coursework is developed in the open on GitHub, at www.github.com/CodeClub/. Come and join us!
Level
Hangman
3
Keep track of your progress by
ticking off the boxes below:
Activity Checklist
1. Find this line in the source code:
word = choice([“code”, “club”])
2. Edit it to add more words. Why not try:
word = choice([“code”, “club”, “robot”, “party”])
Remember to put the words in quotes, and put a comma between them to make a list
of words.
6
These projects are for use inside the UK only. All Code Clubs must be registered. You can check registered clubs on the map at www.codeclub.org.uk.
This coursework is developed in the open on GitHub, at www.github.com/CodeClub/. Come and join us!