Question

Write the function, toppings, that takes a dictionary and modifies it as follows: If the key...

Write the function, toppings, that takes a dictionary and modifies it as follows: If the key "potato" has a value, set that as the value for the key "fries". If the key "salad" has a value, set that as the value for the key "spinach".

the starter code is listed bellow do not change the code complete in Python

def toppings(dictionary):
#Your code here
pass #placeholder statement - delete when you have actual code here

#testing code
dict1 = {'potato': 'ketchup'}
toppings(dict1)
print(dict1) #{'potato': 'ketchup', 'fries': 'ketchup'}

dict2 = {'salad': 'oil', 'potato': 'ketchup'}
toppings(dict2)
print(dict2) #{'spinach': 'oil', 'salad': 'oil', 'potato': 'ketchup', 'fries': 'ketchup'}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

NOTE1: DICTIONARY ORDER IS NOT CONSTANT. SO DON'T CONFUSED WITH OUTPUT

NOTE2: FEEL FREE TO ASK ANY DOUBTS IN THE COMMENT SECTION

CODE

# Method for toppings
def toppings(dictionary):
# Getting keys in dictionary
Allkeys=list(dictionary.keys())

# Loop to check dictionary has given keys
for key in Allkeys:
# Checking keys without case sensitivity
# Checking for potato
if(key.lower()=='potato'):
# if potato is existed
# check potato has a value
if(dictionary[key]!=None):
# if potato has a value, then
# set fries value as potato value
dictionary['fries']=dictionary[key]
# Checking for potato
elif(key.lower()=='salad'):
# if salad is existed
# check salad has a value
if(dictionary[key]!=None):
# if salad has a value, then
# set spinach value as salad value
dictionary['spinach']=dictionary[key]

# Dictionary1 with only potato
dict1={'potato':'ketchup'}
# Calling method for toppings for dictionary2
toppings(dict1)
print(dict1) # Printing dictionary1

# Dictionary2 with two values
dict2={'salad':'oil','potato':'ketchup'}
# Calling method for toppings for dictionary2
toppings(dict2)
print(dict2) # Printing dictionary2

OUTPUT in CONSOLE

CODE in EDITOR

DEAR SIR, PLEASE DON'T FORGET TO GIVE AN UP VOTE IF YOU LIKE THE ANSWER

Thank YOU : )

Add a comment
Know the answer?
Add Answer to:
Write the function, toppings, that takes a dictionary and modifies it as follows: If the key...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python 3 Write a function named inverse that takes a single parameter, a dictionary. In this...

    Python 3 Write a function named inverse that takes a single parameter, a dictionary. In this dictionary each key is a student, represented by a string. The value of each key is a list of courses, each represented by a string, in which the student is enrolled. The function inverse should compute and return a dictionary in which each key is a course and the associated value is a list of students enrolled in that course. For example, the following...

  • Write a function show_value() that takes two arguments: a potential key and a dictionary name. If...

    Write a function show_value() that takes two arguments: a potential key and a dictionary name. If the potential key is in the dictionary, the function should return the value for that key. If the potential key, say invalid_key, is not in the dictionary, it should return the string: invalid_key is not a valid key.

  • using python Write a function that takes a dictionary and a list of keys as 2...

    using python Write a function that takes a dictionary and a list of keys as 2 parameters. It removes the entries associated with the keys from the given dictionary Your function should not print or return anything. The given dictionary should be modified after the function is called Note: it means that you can't create another dictionary in your function. Since dictionaries are mutable, you can modify them directly def remove_keys (dict, key_list) "" "Remove the key, value pair from...

  • PYTHON 3: Write a function removeRand() that takes a dictionary d and an integer n as...

    PYTHON 3: Write a function removeRand() that takes a dictionary d and an integer n as parameters. The function randomly chooses n key-value pairs and removes them from the dictionary d. If n is greater than the number of elements in the dictionary, the function prints a message but does not make any changes to d. If n is equal to the number of elements in d, the function clears the dictionary. Otherwise the function goes through n rounds in...

  • Write a function named "loadStateDict(filename) that takes a filename and returns a dictionary of 2-character state...

    Write a function named "loadStateDict(filename) that takes a filename and returns a dictionary of 2-character state codes and state names. The file has four columns and they are separated by commas. The first column is the state full name and the second column is the state code. You don't have to worry about column 3 & 4. You should eliminate any row that is without a state code. Save the two columns into a dictionary with key = state code...

  • Python 3 Write a function named starCounter that takes three parameters: 1. a dictionary named starDictionary....

    Python 3 Write a function named starCounter that takes three parameters: 1. a dictionary named starDictionary. Each key in starDictionary is the name of a star (a string). Its value is a dictionary with two keys: the string 'distance' and the string 'type'. The value associated with key 'distance' is the distance from our solar system in light years. The value associated with key 'type' is a classification such as 'supergiant' or 'dwarf'. 2. a number, maxDistance 3. a string,...

  • # 1111111111111111111111111111111111111111111111111 # draw_histogram() #-------------------------------------------------- #-------------------------------------------------- """ Define the draw_histogram() function which is passed a Python...

    # 1111111111111111111111111111111111111111111111111 # draw_histogram() #-------------------------------------------------- #-------------------------------------------------- """ Define the draw_histogram() function which is passed a Python dictionary as a parameter. The keys of the dictionary are single letters and the corresponding values are integers, e.g., {'b': 5, 'a': 6, 'c': 3}. For each key:value pair in the dictionary the function prints the key, followed by ": ", followed by a series of stars. The number of stars printed is given by the value corresponding to the key. The keys are...

  • USE PYTHON / RECURSION METHOD Fibonacci Dictionary Function Name: fibtionary Parameters: num (int) Returns: dictionary (key:...

    USE PYTHON / RECURSION METHOD Fibonacci Dictionary Function Name: fibtionary Parameters: num (int) Returns: dictionary (key: int, value: int) Description: You're done with stats, but you still have other math homework to go! You're currently learning about the Fibonacci sequence in math class. The Fibonacci sequence is a series of numbers in the pattern 112 3 5 8 13 21 ..., where the next number is found by adding up the two numbers before it. Write a function that takes...

  • Write a function in python that takes a set A and a relation R(x, y) on...

    Write a function in python that takes a set A and a relation R(x, y) on A (as a python function such that R(x, y) returns true if and only if the relation xRy holds), and returns True if and only if the relation R is reflexive. Here is the function signature you need to use. def is reflexive(A, R): You can test your code as follows. s = {1,2,3} def y(x, y): return x == y def n(x, y):...

  • Write a function called, sameKeys, that takes in two dictionaries. The sameKeys function looks for keys...

    Write a function called, sameKeys, that takes in two dictionaries. The sameKeys function looks for keys that are found in both dictionaries and returns a new dictionary that contains key:value pairs where the key in the new dictionary is the key found in both dictionaries, dictionl and diction2, and the new key's value being a list of the values of dictionl.key and diction2.key values concatenated together. Assumptions 1) 2) Both dictionaries can be empty and an empty dictionary is returned...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT