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 [ ]:
from requests import get

We will get a word list to play the game.

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

First 10 words.

In [ ]:
words[:10]

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 [ ]:
from random import randint

See how it works:

In [ ]:
print(randint(2,10))
print(randint(2,10))
print(randint(2,10))
print(randint(2,10))
print(randint(2,10))

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 [ ]:
def pick_random_word(word_list):
    #write down your code that will return a random word inside the word_list list.
    #use len() function to determine the limist of random integers.
    #also note that index is starting from 0
    return None # you have to change None and make it a rnadom word.

Check out if you function works:

In [ ]:
pick_random_word(words)

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 [ ]:
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
            print('\nCorrect answers!')
        else:
            # ADD CODE 2: increase t value by one if the answer is wrong...
            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 CONDITION: 
            print('\n\nYou WIN!!!')
            break

Run the game:

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

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%