PROBLEM 1 Write a hangman program called hangman.py. Modify your program so that the following functionality is added: Instead of selecting the word from a hard coded list, read input from a file containing a list of words (File: kangaroo, capybara, wombat, koala, wallaby, quokka, platypus, dingo, kookaburra) and store the data (strings) in a list Hint: try using the readline() function or a for loop randomly select a string from the list output / write the selected string to a new file when the game ends, the following information should be outputted to a new file: ● the word ● the state of the hangman ● if the user won or lost Example outputs: quokka O \ | | You won! kangaroo O \ | / | / \ You lost! Afterwards, The following changes should be made to the program: read in a file one line at a time (same as before so far), but instead of simply adding each item to the list, split the string at ‘:’ (.split(“:”)) and add the first string (post-split) as the key and the second string as the value. The key in the dictionary is the word and the value is a hint. if the user enters ‘hint’ (case insensitive) then the hint associated with the word (the value associated with the randomly selected key) should be printed The program should function much the same with just those two changes. This will require adjusting how the word is selected since it is now stored in a dictionary, not a list, adjusting the input filter function to accommodate user input 'hint', adding a function that prints the hint, and adjusting how the data from the file is processed/manipulated. Hint: to randomly select from a dictionary, there are a few different approaches you can take, just remember that to access values in a dictionary you use .get(key) or dictionary[key]. A couple of suggestions might be to also store a list just of the keys, randomly select one of them, and then use the selected word from the list as a reference. However, this requires storing a whole other list, maybe you can think of a more space-efficient way of randomly selecting a key.
PROBLEM 1
Write a hangman program called hangman.py.
Modify your program so that the following functionality is added:
-
Instead of selecting the word from a hard coded list,
-
read input from a file containing a list of words (File: kangaroo, capybara, wombat, koala, wallaby, quokka, platypus, dingo, kookaburra) and store the data (strings) in a list
Hint: try using the readline() function or a for loop
-
randomly select a string from the list
-
output / write the selected string to a new file
-
-
when the game ends, the following information should be outputted to a new file:
● the word
● the state of the hangman
● if the user won or lost
Example outputs:
quokka
O
\ |
|
You won!
kangaroo
O
\ | /
|
/ \
You lost!
Afterwards,
The following changes should be made to the program:
-
read in a file one line at a time (same as before so far), but instead of simply adding each item to the list, split the string at ‘:’ (.split(“:”)) and add the first string (post-split) as the key and the second string as the value. The key in the dictionary is the word and the value is a hint.
-
if the user enters ‘hint’ (case insensitive) then the hint associated with the word (the value associated with the randomly selected key) should be printed
The program should function much the same with just those two changes.
This will require adjusting how the word is selected since it is now stored in a dictionary, not a list, adjusting the input filter function to accommodate user input 'hint', adding a function that prints the hint, and adjusting how the data from the file is processed/manipulated.
Hint: to randomly select from a dictionary, there are a few different approaches you can take, just remember that to access values in a dictionary you use .get(key) or dictionary[key]. A couple of suggestions might be to also store a list just of the keys, randomly select one of them, and then use the selected word from the list as a reference. However, this requires storing a whole other list, maybe you can think of a more space-efficient way of randomly selecting a key.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 7 images