Hangman Game

Download the Hangman Notebook

You need to create Hangman Game in this assignment. Follow the instructions:

We will use requests library to download data from a URL.

In [1]:
from requests import get

We will get a word list to play the game.

In [2]:
words = get('https://itueconomics.github.io/bil113e/assets/words.txt').text
In [3]:
words = words.split()

First 10 words.

In [4]:
words[:10]
Out[4]:
['Aalborg',
 'Aalesund',
 'Aandahl',
 'Aaqbiye',
 'aardvark',
 'aardvarks',
 'aardwolf',
 'aardwolves',
 'Aaronic',
 'Aaronical']

1. Select a random word from the words list

You can use randint function in random library to make random selection.

See documentation: https://docs.python.org/3/library/random.html#random.randint

In [5]:
from random import randint

See how it works:

In [6]:
print(randint(2,10))
print(randint(2,10))
print(randint(2,10))
print(randint(2,10))
print(randint(2,10))
9
5
2
4
2

This part will be graded. You need to write a function that will pick up a random word from a list. Use the hints inside the function.

In [7]:
def pick_random_word(word_list):
    rand_number = randint(0, len(word_list) - 1)
    return word_list[rand_number]

Check out if you function works:

In [8]:
pick_random_word(words)
Out[8]:
'somnambulize'

2. Create the game

Let's assume we have 8 rights to make guess about the words by default. However we will make it changable. So need to create a loop inside the game.

Write down your codes after the comments 1, 2, and 3. Those parts will be graded.

In [9]:
def game(turns = 8):
    word = pick_random_word(words) 
    guess = []
    t = 0
    while t < turns:
        g = input('\nMake a guess: ')
        if g in word:
            # ADD CODE 1: append the correct answer to the guess variable
            guess.append(g)
            print('\nCorrect answers!')
        else:
            # ADD CODE 2: increase t value by one if the answer is wrong...
            t += 1
            print('\nWe do not have {0}'.format(g))
        for i in word:
            if i in guess:
                print(i, end=' ')
                
            else:
                print('_', end=' ')
        #ADD CODE 3: write the condition that checks if all letters are found. use set() function. compare guess and word
        if set(guess) == set(word): 
            print('\n\nYou WIN!!!')
            break

Run the game:

In [10]:
#This part will be graded if it runs well.
game()
Make a guess: a

Correct answers!
_ _ _ _ _ _ _ a 
Make a guess: e

Correct answers!
e _ _ _ e _ _ a 
Make a guess: i

Correct answers!
e _ _ _ e _ i a 
Make a guess: b

We do not have b
e _ _ _ e _ i a 
Make a guess: c

Correct answers!
e c _ _ e _ i a 
Make a guess: o

We do not have o
e c _ _ e _ i a 
Make a guess: r

We do not have r
e c _ _ e _ i a 
Make a guess: t

We do not have t
e c _ _ e _ i a 
Make a guess: n

Correct answers!
e c _ n e _ i a 
Make a guess: d

We do not have d
e c _ n e _ i a 
Make a guess: b

We do not have b
e c _ n e _ i a 
Make a guess: k

We do not have k
e c _ n e _ i a 
Make a guess: l

We do not have l
e c _ n e _ i a 

Grading

  1. pick_random_word(): 25%
  2. ADD CODE 1: 15%
  3. ADD CODE 2: 15%
  4. ADD CODE 3: 20%
  5. If game runs: 25%