Write a python program called anagram.py that does the following: Reads in words
ID: 3913354 • Letter: W
Question
Write a python program called anagram.py that does the following:
Reads in words and their meanings from a text file called “words.txt” (this is on Blackboard). The easiest way to store these would be in a dictionary. You may have a dictionary that looks like the one shown below:
{ “fastidious”:”fussy”, “delighted”:”happy; pleased”, …..}
Alternatively, you could store the words in a list and their meanings in a separate list (in a corresponding location, of course).
Your program should pick a word from the dictionary/list, jumble the letters, and ask the user to unscramble it. The user may type in the unscrambled word or may ask for the definition/meaning of the word by entering a question mark. The game continues until the user says “no” to the question: “Do you want to continue?”. A sample run is shown below:
Unscramble the letters to form a word.
Type '?' for the meaning of the unscrambled word
atgprveeiro
Enter word [? for meaning of unscrambled word]: ?
The word means: privilege
atgprveeiro
Enter word [? for meaning of unscrambled word]: prerogative
You got it! Do you want to continue [yes or no]: yes
Unscramble the letters to form a word.
Type '?' for the meaning of the unscrambled word
ssiouadtfi
Enter word [? for meaning of unscrambled word]: fastidious
You got it! Do you want to continue [yes or no]: no
Links to words.txt: https://drive.google.com/open?id=1oUaaSbvHpU-tXG-rWeCGeYedKlJ_MN4v
Explanation / Answer
First I recommend you correct your word.txt file (the placement of commas and semi-colons and add '{' at beginning and '}' at the end.
This will greatly reduce your work in code.
This will be your new words.txt file.
{
'abhor':'hate',
'bigot':'narrow-minded, prejudiced person',
'counterfeit':'fake; false',
'enfranchise':'give voting rights',
'hamper':'hinder; obstruct',
'kindle':'to start a fire',
'noxious':'harmful; poisonous; lethal',
'placid':'calm; peaceful',
'remuneration':'payment for work done',
'talisman':'lucky charm',
'abrasive':'rough, coarse, harsh',
'bilk':'cheat; defraud'
}
This file is wriiten in dictionary syntax so that you can use eval() and create a dictionary directly from the text.
THE CODE IS AS FOLLOWS:
from random import shuffle, choice
words_file = open('words.txt', 'r')
new_dict = eval(words_file.read())
def shuffle_word(word):
word = list(word)
shuffle(word)
return ''.join(word)
PLAY_CHOICE = True
list_words_keys = list(new_dict)
print('''Unscramble the letters to form a word.
Type '?' for the meaning of the unscrambled word''')
while PLAY_CHOICE:
word = choice(list_words_keys)
shuffled_word = shuffle_word(word)
print(shuffled_word)
user_input = input('Enter word [? for meaning of unscrambled word and quit to quit the game]:')
while user_input:
if user_input == '?':
print('The word means : ', new_dict[word])
print(shuffled_word)
user_input = input('Enter word [? for meaning of unscrambled word and quit to quit the game]:')
elif user_input == word:
print('You got it!')
break
elif user_input == 'quit' or user_input == 'Quit' or user_input == 'QUIT':
PLAY_CHOICE = False
break
else:
print('Oops its wrong!')
print(shuffled_word)
user_input = input('Enter word [? for meaning of unscrambled word and quit to quit the game]:')
I have added quit instead of "Do you want to continue" in order to optimize the code.
Even if you still want to keep your notepad file the old one, then you will have to use regular expressions ("import re") but still you have to edit your words.txt for unexpected semi-colon or commas.
Hope this helped in your question, learning and knowledge
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.