
Create a python program called t9.py. Add the following functions to this program. Write a main() function to call and test all other functions.
Problem 1 (Displaying Dictionaries)
Write a function called display(d) that takes a dictionary as its single input parameter. This function will then print the dictionary to the terminal (in no particular order).
Sample run:
>>> d = {'a':1, 'b':2, 'c':3}
>>> print(d)
{'a': 1, 'b': 2, 'c': 3}
>>> display(d)
a, 1
b, 2
c, 3
Problem 2 [Safe Add]
Write a function called safeAdd(d, k, v, unsafe=False) that takes either three or four input parameters when called. Here, d is a dictionary, k is a key and v is a value. The function will add the key:value pair k:v to the dictionary d if the key k is NOT in the dictionary.
If the key is in the dictionary, the function will display the current value for that key, the new value you passed into the function and ask the user if they wish to proceed and overwrite the existing value.
If the optional fourth parameter is True, then just add the key:value pair without checking if the key is already present.
Sample run (user input is boldened and underlined)
>>> d = {'A':1}
>>> safeAdd(d, 'B', 3}
>>> display(d)
A, 1
B, 3
>>> safeAdd(d, 'B', 2)
The key B is already in the dictionary.
Current value is 3. Do you want to replace this value with 2?
[yes/no] yes
>>> display(d)
A, 1
B, 2
>>> safeAdd(d, 'A', 12)
The key A is already in the dictionary.
Current value is 1. Do you want to replace this value with 12?
[yes/no] no
>>> display(d)
A, 1
B, 2
>>> safeAdd(d,'A', 22, unsafe=True) >>> d['A']
22
Problem 3 (Merging Dictionaries)
Write a function called merge1(d1,d2) that takes two dictionaries as input parameters. This function will create a new dictionary that contains everything from the two input dictionaries and return it. For this problem, you can assume that the input dictionaries do not have any keys in common. Create a few dictionaries, print them to screen using the function created above. After this, merge them using the function you wrote and print the result to the terminal.
Sample run,
>>> d1 = {'A': 100, ‘C':300} >>> d2 = {'B': 200, 'D':400} >>> d3 = merge1(d1,d2)
>>> display(d3)
Problem 4 (Merging Dictionaries Again)
Write a function called merge2(d1,d2) that takes two dictionaries as input parameters. This function will create a new dictionary that contains everything from the two input dictionaries and return it. The same key might appear in both input dictionaries. In the output dictionary, the value for each key will be a list that has all values corresponding to this key in the two input dictionaries.
Sample run,
>>> d1 = {'a': 100, 'b': 200, 'c':300}
>>> d2 = {'a': 300, 'b': 200, 'd':400}
>>> d3 = merge2(d1,d2)
>>> display(d3)
a, [100,300]
b, [200,200]
c, [300]
d, [400]

Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 2 images

- Jupyter Notebook Fixed Income - Certicificate of Deposit (CD) - Compound Interest Schedule An interest-at-maturity CD earns interest at a compounding frequency, and pays principal plus all earned interest at maturity. Write a function, called CompoundInterestSchedule, that creates and returns a pandas DataFrame, where each row has: time (in years, an integer starting at 1), starting balance, interest earned, and ending balance, for an investment earning compoundedinterest. Use a for(or while) loop to create this table. The equation for theith year's ending balance is given by: Ei =Bi (1+r/f)f where: Ei is year i's ending balance Bi is year i's beginning balance (note: B1 is the amount of the initial investment (principal) r is the annual rate of interest (in decimal, e.g., 5% is .05) f is the number of times the interest rate compounds (times per year) The interest earned for a given year is Ei - Bi Note the term of the investment (in years) is not in the above equation; it is used…arrow_forwardq2arrow_forwardAssume a Dictionary structure has been defined that associates letter grades to their respective integer "quality point" values used for calculating a student's grade point average. The name of the structure is gradePoints. Write a statement to retrieve the quality point value for a letter grade of "C". Store the retrieved value in a variable named "qPoints".arrow_forward
- This section has been set as optional by your instructor. OBJECTIVE: Write a program in Python that prints the middle value of a dictionary's values, using test_dict from the test_data.py module. NOTE: test_dict is created & imported from the module test_data.py into main.py Example 1: test dict = {'a':1,'b':9,'c':2,'d':5} prints 5 Example 2: test_dict = {'b':9,'d':5} prints 5 Hint: You may(or may not) find the following function useful: list.sort() which sorts the elements in a list. Note: the result is automatically stored in the original list, and return value from the function is None Туре list = [1, 2,3,2,1] After calling: list = [1,1,2,2,3] list.sort(), 339336.2266020 x3zgy7 LAB 13.11.1: Middlest Value (Structured Types) АCTIVITY 0/11 File is marked as read only Current file: test_data.py 1 #This module creates the test_dict to be used in main.py 2 #Reads input values from the user & places them into test_dict 3 dict_size = int(input()) #Stores the desired size of test_dict. 4…arrow_forwardMust show it in Python: Please show step by step with comments. Please show it in simplest form. Input and Output must match with the Question Please go through the Question very carefully.arrow_forwardIn Python, create a telephone dictionary where the user is asked whether they wish to add, remove,modify, search for a telephone number (given the name), or exit. Use functions for each one ofthe four different operations. Use an infinite looparrow_forward
- JS Write a function named sum_between_indices whose parameter is a list/array of decimal numbers. Your function must return the sum of the entries at index 4 through index 15. Make certain to include the entries at index 4 and at index 15 in your sum. You SHOULD assume the parameter will have more than 15 entries.arrow_forwardRewrite the determineAdmission Fee function using a dictionary data structure such that it produces the same output when the program is run. #Determine an admission fee based on age group. def main(): print("Enter the person's age group ", end=""") ageGroup=input("(child, minor, adult, or senior): " print("The admission fee is", determineAdmission Fe def determineAdmission Fee(ageGroup): if ageGroup == "child": return 0 elif ageGroup == "minor": return 5 # age 6 to 17, $5 elif ageGroup == "adult": return 10 # age 18 to 64, $10 elif ageGroup == "senior": return 8 # age >= 65, $8arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY





