Question

Create a Python class named Phonebook with a single attribute called entries. Begin by including a...

  1. Create a Python class named Phonebook with a single attribute called entries. Begin by including a constructor that initializes entries to be an empty dictionary.

  2. Next add a method called add_entry that takes a string representing a person’s name and an integer representing the person’s phone number and that adds an entry to the Phonebook object’s dictionary in which the key is the name and the value is the number. For example:

    >>> book = Phonebook()
    >>> book.add_entry('Turing', 6173538919)
    
  3. Add a method named contains to your Phonebook class. It should take a parameter name and return True if name is present in the phonebook, and False otherwise. For example:

    >>> book = Phonebook()
    >>> book.contains('Turing')
    False
    >>> book.add_entry('Turing', 6173538919)
    >>> book.contains('Turing')
    True
    
  4. Write another method for your Phonebook class called number_for that takes a parameter name and returns the phone number for name in the called Phonebook object. It should return -1 if name is not found. Here is an example:

    >>> book = Phonebook()
    >>> book.add_entry('Turing', 6173538919)
    >>> book.add_entry('Hopper', 6174951000)
    >>> book.number_for('Turing')
    6173538919
    >>> book.number_for('Hopper')
    6174951000
    >>> book.number_for('Codd')
    -1
    
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

Output:

Raw Code:

class Phonebook:
   def __init__(self): # Constructor
       self.entries = {} # Initializing entries with an empty dictionary
   def add_entry(self,name,number):
       self.entries[name] = number # adding new entry to entries dictionary
   def contains(self,name):
       if name in self.entries: # Checking if the name is in entries dictionary or not
           return True
       else:
           return False
   def number_for(self,name):
       if self.contains(name): # Checking if the name is in entries dictionary or not
           return self.entries[name] # If the given name is in entries dictionary then it return the phone number for that name.
       else:
           return -1
def main():
   book = Phonebook() # Creating the object for Phonebook class
   # Calling methods and printing the return values
   print(book.contains('Turing'))
   book.add_entry('Turing',6173538919) # Adding new entry
   print(book.contains('Turing'))
   book.add_entry('Hopper',6174951000) # Adding new entry
   print("Number for Turing: " + str(book.number_for('Turing')))
   print("Number for Hopper: " + str(book.number_for('Hopper')))
   print("Number for Codd : "+ str(book.number_for('Codd')))

main() # Calling main function.

If you have any doubts feel free to comment.

Add a comment
Know the answer?
Add Answer to:
Create a Python class named Phonebook with a single attribute called entries. Begin by including a...
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
  • Create a class called AddressBook. It should be able to store a maximum of 50 names...

    Create a class called AddressBook. It should be able to store a maximum of 50 names and 50 phone numbers (make an array of structures in the object or two arrays). It should contain a method called addName that will take a name and phone number and add it to the address book. There should be a method called findName that will take a string for the name and return the phone number if found in the list otherwise return...

  • Write a class called Player that has four data members: a string for the player's name,...

    Write a class called Player that has four data members: a string for the player's name, and an int for each of these stats: points, rebounds and assists. The class should have a default constructor that initializes the name to the empty string ("") and initializes each of the stats to -1. It should also have a constructor that takes four parameters and uses them to initialize the data members. It should have get methods for each data member. It...

  • Make a public class called ManyExamples and in the class... Write a function named isEven which...

    Make a public class called ManyExamples and in the class... Write a function named isEven which takes in a single int parameter and returns a boolean. This function should return true if the given int parameter is an even number and false if the parameter is odd. Write a function named close10 which takes two int parameters and returns an int. This function should return whichever parameter value is nearest to the value 10. It should return 0 in the...

  • A java class named Book contains: instance data for the title, author, publisher and copyright year...

    A java class named Book contains: instance data for the title, author, publisher and copyright year a constructor to accept and initialize the instance data variables set and get methods a toString() method to return a formatted, multi-line description of the book Produce a child class that inherits from Book. The class is called Dictionary. Dictonary must include: instance data that describes the number of words in the dictionary a constructor that takes in all information needed to describe a...

  • Create a class called DuplicateRemover. Create an instance method called remove that takes a single parameter...

    Create a class called DuplicateRemover. Create an instance method called remove that takes a single parameter called dataFile (representing the path to a text file) and uses a Set of Strings to eliminate duplicate words from dataFile. The unique words should be stored in an instance variable called uniqueWords. Create an instance method called write that takes a single parameter called outputFile (representing the path to a text file) and writes the words contained in uniqueWords to the file pointed...

  • Create a Python script file called hw12.py. Add your name at the top as a comment,...

    Create a Python script file called hw12.py. Add your name at the top as a comment, along with the class name and date. Ex. 1. a. Texting Shortcuts When people are texting, they use shortcuts for faster typing. Consider the following list of shortcuts: For example, the sentence "see you before class" can be written as "c u b4 class". To encode a text using these shortcuts, we need to perform a replace of the text on the left with...

  • Using JAVA* Design a class named Person with fields for holding a person’s name, address, and...

    Using JAVA* Design a class named Person with fields for holding a person’s name, address, and telephone number. Write one or more constructors and the appropriate mutator and accessor methods for the class’s fields. Next, design a class named Customer, which extends the Person class. The Customer class should have a field for a customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write one or more constructors and the appropriate mutator...

  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

  • Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static...

    Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static void main(String[]args)throws IOException { PhoneBook obj = new PhoneBook(); PhoneContact[]phBook = new PhoneContact[20]; Scanner in = new Scanner(System.in); obj.acceptPhoneContact(phBook,in); PrintWriter pw = new PrintWriter("out.txt"); obj.displayPhoneContacts(phBook,pw); pw.close(); } public void acceptPhoneContact(PhoneContact[]phBook, Scanner k) { //void function that takes in the parameters //phBook array and the scanner so the user can input the information //declaring these variables String fname = ""; String lname = ""; String...

  • python code? 1. Create a class called Person that has 4 attributes, the name, the age,...

    python code? 1. Create a class called Person that has 4 attributes, the name, the age, the weight and the height [5 points] 2. Write 3 methods for this class with the following specifications. a. A constructor for the class which initializes the attributes [2.5 points] b. Displays information about the Person (attributes) [2.5 points] c. Takes a parameter Xage and returns true if the age of the person is older than Xage, false otherwise [5 points] 3. Create another...

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