PYTHON:In this problem, you will write a program which uses a dictionary to store phone numbers of contacts. You will name your dictionary contacts. It will store the names as keys and phone numbers...

PYTHON:In this problem, you will write a program which uses a dictionary to store phone numbers of contacts. You will name your dictionary contacts. It will store the names as keys and phone numbers as values. Your program should have the following functions: (a) Write a function add_entry(contacts, name, number) that adds an entry to the dictionary contacts. def add_entry( contacts , name , number ): """ sig :dict ( str , str ) , str , str ->NoneType """ The function takes a dictionary, contacts, a name as a string and a number as a string. It adds an entry to contacts with the name as the key and the number as the value. The item should only be added to the dictionary if the following is true: i. An entry in contacts with the key name does not exist already. ii. Number is a valid phone number. A valid phone number is a string with 10 digits. For example, ‘2014567890’ is a valid phone number. But ‘201a45b789’ or ‘20145678’ are both invalid. Your function should display an error message if the entry cannot be added. Test your function with valid and invalid numbers and duplicate names to ensure that it works correctly. (b) Write a function lookup(contacts, name). This function should return the phone number associated with name in contacts. If the input name is not found in contacts, display an error message that indicates so. Test your function with valid and invalid inputs to ensure that it works correctly. def lookup ( contacts , name ): """ sig :dict ( str , str ) , str -> str """ (c) Write a function print_all(contacts) that prints all the entries in contacts. Your function should print both the name and the phone number. def print_all( contacts ): """ sig :dict ( str , str ) - >NoneType """ For example, the output of contacts could be: Bob Harrison Alice Graves Mary Johnson 1234567890 2345678901 3456789012 Use the tab character "\t" for spacing between the name and phone number. Test your function to ensure that it works properly. (d) Write a function add_from_file(filename, contacts) which reads names and phone numbers from a file and enters them in the contacts dictionary. The file ‘us-senate.csv’ has been provided for you. Each line in the file contains a name followed by a phone number. The names are in the form ‘LastName, FirstName.’ Note that the phone numbers are in a format different from the format that is required by your dictionary. Hint: The string method replace() might be useful here. The function add_from_file(filename, contacts) should make a call to the function add_entry(contacts, name, phone number) to add entries to the contacts dictionary from the information in the file. The name as represented in your dictionary should be in the format ”FirstNameLastName”. For example, an entry in your dictionary might be ‘Charles Schumer’: ‘2022242841’. Use a try-except block when opening the file. Handle a potential FileNotFoundError by giving a message that the file is not found. Test your function to ensure that it works properly. def add_from_file( filename , contacts ): """ sig : str , dict ( str , str ) - >NoneType """ (e) Write a main function to test your program. The program should make a call to your function add_from_file(filename, contacts) in order to create the dictionary. Be sure to also test your functions add_entry(contacts, name, number) and lookup(contacts, name) from main to ensure that they produce the correct action or give an error message. Also, make a call to your function print_all(contacts) to ensure that it prints out the dictionary as expected. For example, the output from a call to main could be as follows: Mickey Mouse already exists in contacts and can’t be added again can’t add Minnie Mouse to contacts because 123 is not a valid phone number The number for Charles Schumer is : 2022242841 The number for Kirsten Gillibrand is : 2022244451 Mickey Mouse 1234567890 Daniel Sullivan 2022243004 Lisa Murkowski 2022246665 Gordon Jones 2022244124 Richard Shelby 2022245744 John Boozman 2022244843 Tom Cotton 2022242353 Jeff Flake 2022244521 John McCain 2022242235 Dianne Feinstein 2022243841 Kamala Harris 2022243553 Cory Gardner 2022245941 Michael Bennet 2022245852 Christopher Murphy 2022244041 Richard Blumenthal 2022242823 Christopher Coons 2022245042 Thomas Carper 2022242441 Bill Nelson 2022245274 Marco Rubio 2022243041 David Perdue 2022243521 Johnny Isakson 2022243643 Brian Shcatz 2022246542 Mazie Hirono 2022246361 Charles Grassley 2022243744 Joni Ernst 2022243254 James Risch 2022242752 Mike Crapo 2022246142 Richard Durbin 2022242152 Tammy Duckworth 2022242854 Joe Donnelly 2022244814 Todd Young 2022245623 Jerry Moran 2022246521 Pat Roberts 2022244774 Mitch McConnell 2022242541 Rand Paul 2022244343 Bill Cassidy 2022245824 John Kennedy 2022244623 Edward Markey 2022242742 Elizabeth Warren 2022244543 Benjamin Cardin 2022244524 Chris Van Hollen 2022244654 Angus King 2022245344 Susan Collins 2022242523 Debbie Stabenow 2022244822 Gary Peters 2022246221 Tina Smith 2022245641 Amy Klobuchar 2022243244 Claire McCaskill 2022246154 Roy Blunt 2022245721 Roger Wicker 2022246253 Thad Cochran 2022245054 Jon Tester 2022242644 Steve Daines 2022242651 Richard Burr 2022243154 Thom Tillis 2022246342 Heidi Heitkamp 2022242043 John Hoeven 2022242551 Ben Sasse 2022244224 Deb Fischer 2022246551 Jeanne Shaheen 2022242841 Margaret Hassan 2022243324 Cory Booker 2022243224 Robert Menendez 2022244744 Martin Heinrich 2022245521 Tom Udall 2022246621 Catherine Masto 2022243542 Dean Heller 2022246244 Charles Schumer 2022242841 Kirsten Gillibrand 2022244451 Rob Portman 2022243353 Sherrod Brown 2022242315 James Inhofe 2022244721 James Lankford 2022245754 Jeff Merkley 2022243753 Ron Wyden 2022245244 Patrick Toomey 2022244254 Robert Casey 2022246324 Jack Reed 2022243542 Sheldon Whitehouse 2022242921 Lindsey Graham 2022245972 Tim Scott 2022246121 John Thune 2022242321 Mike Rounds 2022245842 Bob Corker 2022243344 Lamar Alexander 2022244944 John Cornyn 2022242934 Ted Cruz 2022245922 Mike Lee 2022245444 Orrin Hatch 2022245251 Mark Warner 2022242023 Tim Kaine 2022244024 Bernard Sanders 2022245141 Patrick Leahy 2022244242 Maria Cantwell 2022243441 Patty Murray 2022242621 Ron Johnson 2022245323 Tammy Baldwin 2022245653 Joe Manchin 2022243954 Shelley Capito 2022246472 John Barrasso 2022246441 Michael Enzi 2022243424
Nov 14, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here