Question: Write a recursive function that checks to see if a key is in a dictionary. This function should return a boolean indicating whether or



Write a recursive function that checks to see if a key is in a dictionary. This function should return a boolean indicating whether or not the key is present at any level of the dictionary. Think of dictionaries as being of these shapes: d1 = { "a": 1 } d2 = { "b": ( } } d3 = { J "a": "Test" "c": { 1 "z": ["'blue', 'green', 'red' Dictionaries can be nested, but there will only ever be 1 key at each level. The value of these keys can be anything, a string, a number, a list, another dictionary. Only if the value is a dictionary do you have to recursively check if the key exists in the nested dictionary. Use the following function header: isKeyInDict(key, obj) When your program is executed you should do the following: Use Python's Input function to execute your code. When your file is executed, prompt the user as follows: >> What would you like to do? Choose from "execute isKeyInDict" or "write to file" Based on the user's feedback, you will either output the result of isKeyInDict for a set of examples that you stored previously in variables. Make sure to test your function with inputs of multiple levels and some where the key is present and other where the key is not present. Helpful hints: Remember that the user may not type the commands ("execute isKeylnDict" or "write to file") exactly as you ask. While we can't predict what the user meant if their input is way off, there is at least 1 way we can help. Your code should account for any uppercase/lowercase lettering deviation from your instruction. For example, your code should be able to understand "EXECUte iskeyindict". If the user entered "write to file" Then your code should continue to ask the user a couple more questions: 1. What filename would the user like the file to have? 2. What message would the user like to write to the file? 3. If the filename provided by the user already exists, you should ask a 3rd question: Does the user wish to overwrite the contents of the existing file with the message given on number 2 or should the message simply be appended to existing contents of the file? If the user entered something else in the prompt, end the program with a message indicating that you can not understand the user's instruction.
Step by Step Solution
3.45 Rating (164 Votes )
There are 3 Steps involved in it
Heres the code for the recursive function that checks if a key is in a dictionary def isKeyInDictkey ... View full answer

Get step-by-step solutions from verified subject matter experts