In: Computer Science
use the python language and fix the error code
#Write a function called rabbit_hole. rabbit_hole should
have
#two parameters: a dictionary and a string. The string may be
#a key to the dictionary. The value associated with that key,
#in turn, may be another key to the dictionary.
#
#Keep looking up the keys until you reach a key that has no
#associated value. Then, return that key.
#
#For example, imagine if you had the following dictionary.
#This one is sorted to make this example easier to follow:
#
# d = {"bat": "pig", "pig": "cat", "cat": "dog", "dog":
"ant",
# "cow": "bee", "bee": "elk", "elk": "fly", "ewe": "cod",
# "cod": "hen", "hog": "fox", "fox": "jay", "jay": "doe",
# "rat": "ram", "ram": "rat"}
#
#If we called rabbit_hole(d, "bat"), then our code should...
#
# - Look up "bat", and find "pig"
# - Look up "pig", and find "cat"
# - Look up "cat", and find "dog"
# - Look up "dog", and find "ant"
# - Look up "ant", and find no associated value, and so it
would
# return "ant".
#
#Other possible results are:
#
# rabbit_hole(d, "bat") -> "fly"
# rabbit_hole(d, "ewe") -> "hen"
# rabbit_hole(d, "jay") -> "doe"
# rabbit_hole(d, "yak") -> "yak"
#
#Notice that if the initial string passed in is not a key in
#the dictionary, that string should be returned as the result
as
#well.
#
#Note, however, that it is possible to get into a loop. In
the
#dictionary above, rabbit_hole(d, "rat") would infinitely go
#around between "rat" and "ram". You should prevent this: if
a
#key is ever accessed more than once (meaning a loop has been
#reached), return the boolean False.
#
#Hint: If you try to access a value from a dictionary that
does
#not exist, a KeyError will be raised.
#Write your function here!
def rabbit_hole(d,word):
for key in d.keys():
if word == key:
new_key = d[word]
word = new_key
continue
elif word != key:
pass
return d[word]
#Below are some lines of code that will test your
function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print: ant, hen, doe, yak, False, each on their own line.
d = {"bat": "pig", "pig": "cat", "cat": "dog", "dog": "ant",
"cow": "bee", "bee": "elk", "elk": "fly", "ewe": "cod",
"cod": "hen", "hog": "fox", "fox": "jay", "jay": "doe",
"rat": "ram", "ram": "rat"}
print(rabbit_hole(d, "bat"))
print(rabbit_hole(d, "ewe"))
print(rabbit_hole(d, "jay"))
print(rabbit_hole(d, "yak"))
print(rabbit_hole(d, "rat"))
Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code. Below is the output of the program: Below is the code to copy: #CODE STARTS HERE----------------
def rabbit_hole(d,word): #function rabbit_hole traversed = [] #Used to store all the words that were searched try: #Try , except block to catch key value errors while True: #infinite loop is used to travrl across the word chain if word not in traversed: #Checking if word was not searched before traversed.append(word) #Add word to traversed list word = d[word] #Change word for next iteration else: #If word repeats, then its a closed chain return False #return false except KeyError: #This catches key errors return word #This is the end of chain def main(): d = {"bat": "pig", "pig": "cat", "cat": "dog", "dog": "ant", "cow": "bee", "bee": "elk", "elk": "fly", "ewe": "cod", "cod": "hen", "hog": "fox", "fox": "jay", "jay": "doe", "rat": "ram", "ram": "rat"} print(rabbit_hole(d, "bat")) print(rabbit_hole(d, "ewe")) print(rabbit_hole(d, "jay")) print(rabbit_hole(d, "yak")) print(rabbit_hole(d, "rat")) main() #CODE ENDS HERE-----------------