You need to create Hangman Game in this assignment. Follow the instructions:
We will use requests
library to download data from a URL.
from requests import get
We will get a word list to play the game.
words = get('https://itueconomics.github.io/bil113e/assets/words.txt').text
words = words.split()
First 10 words.
words[:10]
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
from random import randint
See how it works:
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.
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:
pick_random_word(words)
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.
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:
#This part will be graded if it runs well.
game()
pick_random_word(): 25%
ADD CODE 1: 15%
ADD CODE 2: 15%
ADD CODE 3: 20%
If game runs: 25%