172k views
1 vote
Kenneth bought a new phone and added two of his friends' numbers to his phonebook. However, he forgot to transfer the phonebook from his previous phone beforehand. Help Kenneth keep the most up-to-date phone numbers for all his friends on his new device. That is, if there is a number saved on both old and new devices for the same friend, you should keep the number saved on the new phone; or if there is only one phone number for a friend, you should keep it, regardless of which device contains it.

User Makan
by
4.5k points

1 Answer

4 votes

Answer:

The program in Python is as follows:

phonedirs = {'Maegan': 1 , 'McCulloch': 2, 'Cindy': 3}

for i in range(2):

name = input("Name: ")

phonenum = int(input("Phone: "))

phonedirs [name] = phonenum

print(phonedirs)

Step-by-step explanation:

Given

The instruction in the question is incomplete.

See attachment for complete question

Required

Write a code that carries out the 4 instructions in the attachment

See answer section for solution.

The explanation is as follows:

(1) This initializes the phone book

phonedirs = {'Maegan': 1 , 'McCulloch': 2, 'Cindy': 3}

The following is repeated for the two friends

for i in range(2):

(2) This gets the name of each friend

name = input("Name: ")

(2) This gets the phone number of each friend

phonenum = int(input("Phone: "))

(3) This updates the phone book with the inputs from the user

phonedirs[name] = phonenum

(4) This displays the updated phone book

print(phonedirs)

Kenneth bought a new phone and added two of his friends' numbers to his phonebook-example-1
User Andre Dublin
by
5.3k points