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]
3 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]3 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
In python:
Misha Larkins [42]

Answer:

Following are the code to this question:

list_val = input()#defining a integer variable for input value

test_grades = list(map(int, list_val.split()))#defining test_grades as a list

sum_extra = -999 #defining sum_extra that holds negative integer value

sum_extra = 0#defining sum_extra that holds value

for y in range(len(test_grades)):#defining a for loop to check range of list  

   if(test_grades[y] > 100):# defining if block that check list value is greater then 100

       sum_extra = sum_extra + (test_grades[y] - 100)#use sum_extra variable to hold extra value and add this value

print('Sum extra:', sum_extra)#print value

Output:

101 83 107 90

Sum extra: 8

Explanation:

In the above code a, "list_val" variable is declared, that uses an input method to input the values and declared a "test_grades" variable that uses a list method to add all values in the list.

In the next step, the "sum_extra" variable is declared, which holds some values and defines a for loop to check the range of the "test_grades", and define a if block, that checks list value is greater than 100. If the condition is true, it will remove the extra value, and add it into the sum_extera variable and add its value, and at the last use, print variable to print its value.

6 0
3 years ago
Which of the following CANNOT be done in Normal view A. Enter bolded text in the Notes Pane B. Delete previously entered notes i
blondinia [14]

Answer:

<em>Well, Your answer will be is </em><em>B. Delete previously entered notes in the Notes Pane. Good Luck!</em>

3 0
3 years ago
Fredrico has to lead a training for his company. He wants to include a visual element to help engage his audience. What type of
andreyandreev [35.5K]
Presentation software is probably his best option. It would allow him to present visual and audio supplements to his audience.
8 0
3 years ago
Read 2 more answers
What are the 4 main types of parking
erik [133]
Parallel parking, emergency parking, parking on a hill, and prohibited parking. at least that's what the handbook in Arizona says, but check with your state handbook.)
6 0
3 years ago
Who is your favorite MHA Character? Or what anime show of your favorite character
adoni [48]

Answer:

Many of them are interesting, and relatable too.

5 0
3 years ago
Other questions:
  • This toolbar has icons representing the application's basic operations such as Save and Copy. Drawing Formatting Standard Task
    11·2 answers
  • There is a flashing yellow light at the intersection you are approaching. What does the flashing yellow light indicate, and what
    8·1 answer
  • Text that does not fit in a cell
    7·1 answer
  • Lisa wants to send an email with some confidential Information. Which of these options would work best for her?
    6·1 answer
  • Darian has a gourmet cupcake business and needs a website to compete with the other bakeries in his area. He has a Google My Bus
    15·1 answer
  • What are the tasks of a doc file
    15·1 answer
  • Which of the following is not the name of a java wrapper class from the Java API?
    14·1 answer
  • After reading the article, "The Impact of Technology", answer the following question.
    7·1 answer
  • When pointed over a text within a paragraph, the cursor takes the shape of a/an
    10·1 answer
  • Could anyone help me with this assignment please?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!