PYTHON PROGRAMMING Write a python program called anagram.py that does the follow
ID: 3913042 • Letter: P
Question
PYTHON PROGRAMMING
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
Link to the file: https://drive.google.com/open?id=1oUaaSbvHpU-tXG-rWeCGeYedKlJ_MN4v
Explanation / Answer
import random
with open("inputWords.txt") as f:
lines = f.readlines()
d={}
word=[]
mean=[]
i=0;
for line in lines:
data=line.split(":")
word.append(data[0].strip(' '))
mean.append(data[1].strip(' '))
# print(word)
# print(mean)
num=random.random()
index=int(num*len(word))
# shuffled=random.shuffle(list(word[index]))
# print(shuffled)
# print(word[index])
l =list(word[index])
random.shuffle(l)
shuffled=''.join(l)
# print(shuffled)
while True:
choice=input("Do you want to continue?")
if choice=="no":
break;
else:
num=random.random()
index=int(num*len(word))
l =list(word[index])
random.shuffle(l)
shuffled=''.join(l)
# while True:
print(shuffled)
action=input("Enter word [? for meaning of unscrambled word]: ")
if action=="?":
print(mean[index])
action=input("Enter word [? for meaning of unscrambled word]: ")
if action==word[index]:
print("You got it! ")
elif action==word[index]:
print("You got it! ")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.