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...

  • Course Class Create a class called Course. It will not have a main method; it is...

    Course Class Create a class called Course. It will not have a main method; it is a business class. Put in your documentation comments at the top. Be sure to include your name. Course must have the following instance variables named as shown in the table: Variable name Description crn A unique number given each semester to a section (stands for Course Registration Number) subject A 4-letter abbreviation for the course (e.g., ITEC, PHED, CHEM) number A 4-digit number associated...

  • I need to create file called EvilPerson.java. This implements a class that inherits from Person. The...

    I need to create file called EvilPerson.java. This implements a class that inherits from Person. The EvilPerson class doesn’t want you to be able to set a phone number for a user, so it always stores the text “n/a” in the phone attribute. You should define a constructor for EvilPerson that takes in a name and a phone number, stores the name correctly but throws away the phone number values and stores “n/a” there instead. The setPhone() method from Person...

  • 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 two dimensional arrays Create a method named curiousNumber that takes an integer named num as...

    Java two dimensional arrays Create a method named curiousNumber that takes an integer named num as a parameter and returns a boolean. You can assume the num will be a positive integer. The curiousNumber method should determine whether num is a curious number. A curious number is a number that is equal to the sum of the factorial of each of its digits. For example, 145 is a curious number because 145 = 1! + 4! + 5!. sample: boolean...

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