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):
rand_number = randint(0, len(word_list) - 1)
return word_list[rand_number]
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
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:
#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%