1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
mote1985 [20]
2 years ago
12

In this exercise, you will get some practice with the __add__ method by implementing it for a class called ContactBook. This cla

ss represents a collection of names and phone numbers. ContactBook stores its information as a dictionary, where the key is a name and the value is a phone number or group of phone numbers. The keys and values in this dictionary are stored as strings. When printed, a ContactBook might look like this:
Computers and Technology
1 answer:
Wittaler [7]2 years ago
8 0

Answer:

class ContactBook():

   def __init__(self):

       self.contacts ={}

   def __repr__(self):

       return str(self.contacts)

   def add_contact(self,name,number):

       self.contacts[name] = number

   def __add__(self, other):

       new_contact = ContactBook()

       other_contact = other.contacts.keys()

       for name,num in self.contacts.items():

           if name in other_contact:

               new_contact.add_contact(name,num or other_contact[name])

           else:

               new_contact.add_contact(name,num)

       for name,num in other.contacts.items():

           if name not in self.contacts:

               new_contact.add_contact(name, num)

       return new_contact-

cb1 = ContactBook()

cb2 = ContactBook()

cb1.add_contact('Jonathan','444-555-6666')

cb1.add_contact('Puneet','333-555-7777')

cb2.add_contact('Jonathan','222-555-8888')

cb2.add_contact('Lisa','111-555-9999')

print(cb1)

print(cb2)

cb3 = cb1+cb2

print(cb3)

Explanation:

The ContactBook class holds the contact details of an instance of the class. The class has three magic methods the '__repr__', '__init__', and the '__add__' which is the focus of the code. The add magic method in the class adds the contact book (dictionary) of two added object instance and returns a new class with the contact details of both operand instances which is denoted as self and other.

You might be interested in
Please help me !!
gavmur [86]

e. uhmmm

19+21=30

like....

easy.

What grade class is this? I wanna do it cause it easy AFef

6 0
2 years ago
A leading global vendor of computer software, hardware for computer, mobile and gaming systems, and
V125BC [204]

Answer:

Microsoft

Explanation:

5 0
2 years ago
What is the main idea of this article? Please someone help me. I will give brainliest answer
mart [117]
The main idea of this article is that theme parks are conducting scientific research that is benefiting the community. Hope this helps!
8 0
3 years ago
"what windows process is responsible for authenticating users?"
Likurg_2 [28]
Lsass.exe /................................................
8 0
3 years ago
Disadvantages assembly level language for programming ​
Alisiya [41]
It takes a lot of time and effort to write the code for the same. It is very complex and difficult to understand. The syntax is difficult to remember. It has a lack of portability of program between different computer architectures.

Hope that helps
7 0
3 years ago
Read 2 more answers
Other questions:
  • Survey Q. Non-scoring: What role is played in the team? (1 correct answer)
    14·1 answer
  • You have a Bluetooth headset that integrates with your computer so that you can talk to partners through Microsoft Lync. This is
    6·2 answers
  • A tower or mini tower pc is a type of all- in -one unit true or false
    9·2 answers
  • What is the relationship between a method and a function
    11·2 answers
  • Which of the following describes ETL? a) A process that transforms information using a common set of enterprise definitions b) A
    14·1 answer
  • You work in the Accounting department and have been using a network drive to post Excel workbook files to your file server as yo
    15·1 answer
  • This exercise shows why each pivot (in eli1nination by pivoting) must be in a different row. (a) In Example 7, make the third pi
    15·1 answer
  • Name the application used for creating Presentations___
    5·2 answers
  • Mr. Simmons has assigned a research project. Every student in the class will create a single page report about the recycling hab
    15·1 answer
  • What types of things were often NOT captured in early photographs?
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!