Python Question. Can you explain thoroughly Here is a text file, called animals.
ID: 3844217 • Letter: P
Question
Python Question. Can you explain thoroughly
Here is a text file, called animals.txt. Dog goes woof Cat goes meow Bird goes tweet and mouse goes squeek Cow goes moo Frog goes croak and the elephant goes toot Ducks say quack and fish go blub and the seal goes ow ow ow But there's one sound - That no one knows What does the fox say? The file is stored in the same directory as a Python program, shown below. mydict = {} myfile = open("animals.txt", 'r') for line in myfile: line = line.rstrip() words = line.split() count = 0 for word in words: if word == "goes" or word == "say" or word == "go": mydict[words[count - l].lower()] = words[count + 1] count += 1 myfile.close () if "fox" in mydict.keys(): print("What does the fox say?") print("The answer is:", mydict["fox"]) else: print("It will remain a mystery ellipsis") What does the program display when it has been executed?Explanation / Answer
mydict={}
myfile=open("animals.txt",'r')
for line in myfile:
line=line.rstrip()
words=line.split()
count=0
for word in words:
if word=="goes" or word=="say" or word=="go":
mydict[words[count-1].lower()]=words[count+1]
count+=1
myfile.close()
if "fox" in mydict.keys():
print("What does the fox say?")
print("The answer is:",mydict["fox"])
else:
print("It will remain a mystery...")
The above program displays the following output:
It will remain a mystery...
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.