Question

In: Computer Science

Dictionaries in python can store other complex data structures as the VALUE in a (KEY, VALUE)...

Dictionaries in python can store other complex data structures as the VALUE in a (KEY, VALUE) pair.

2.1) Create an empty dictionary called 'contacts'.

The KEY in this dictionary will be the name of a contact (stored as a string). The VALUE in this dictionary will be a list with three elements: the first element will be a phone number (stored as a string), the second an email address (stored as a string) and the third their age (stored as an integer).

2.2) Insert information for three imaginary contacts of yours in the dictionary contacts.

2.3) Using input(), ask the user to enter an age. Then, using a for loop and (inside the for loop) an if statement, print the name and phone number for all contacts that are either of that age or older. If no contacts of or above that age are found, your code should report that to the user. For example, suppose you have three friends in contacts with the data:

 Name:  Julia
 Phone: 384-493-4949 
 Email: [email protected] 
 Age:   34

 Name:  Alfred
 Phone: 784-094-4520 
 Email: [email protected] 
 Age:   49

 Name:  Sam
 Phone: 987-099-0932 
 Email: [email protected]
 Age:   28

Then, your code asks the user to specify a minimum age. In the example below, the user enters the age '30':

 What is the minimum age? 30

Your code should then print the name and phone number of everyone who is 30 years old or older:

 Julia is 34 years old and can be reached at 384-493-4949. 
 Alfred is 49 years old and can be reached at 784-094-4520.

If the user were to enter (for example) 50 at the prompt above, your code should have instead printed the message:

 Sorry, you do not have any contacts that are 50 or older.

Solutions

Expert Solution

CODE

# creating empty dictionary
contacts = {}

# adding 3 contacts to the dictionary
contacts["Julia"] = ["384-493-4949", "[email protected]", 34]
contacts["Alfred"] = ["784-094-4520", "[email protected]", 49]
contacts["Sam"] = ["987-099-0932", "[email protected]", 28]

# reading age from user
age = int(input("What is the minimum age: "))

# setting found to 0
found = 0

# retrieving each record from the dictionary
for key in contacts:
        # checking if any age in contacts are greater than or equal to user given age
        if(contacts[key][2] >= age):
                # if yes, then prints the output
                print(key, "is", contacts[key][2], "years old can be reached at", contacts[key][0], ".")
                # setting flag to 1
                found = 1
# checking if found is 0, if yes it prints not contact found
if(found == 0):
        print("Sorry, you do not have any contacts that are", age, "or older.")

CODE SCREENSHOT

OUTPUT


Related Solutions

Describe how tuples can be useful with loops over lists and dictionaries, and give Python code...
Describe how tuples can be useful with loops over lists and dictionaries, and give Python code examples. Create your own code examples. Do not copy them from the textbook or any other source. Your descriptions and examples should include the following: the zip function, the enumerate function, and the items method.
"Python lists are power data structures. Describe some of the benefits of Python lists" Answer the...
"Python lists are power data structures. Describe some of the benefits of Python lists" Answer the question above in a few sentences.
"Python lists are power data structures. Describe some of the benefits of Python lists" Make sure...
"Python lists are power data structures. Describe some of the benefits of Python lists" Make sure you don't just give me a list but a few sentences instead just answering it.
Using Python 3. Extract the value associated with the key color and assign it to the...
Using Python 3. Extract the value associated with the key color and assign it to the variable color. Do not hard code this. info = {'personal_data': {'name': 'Lauren', 'age': 20, 'major': 'Information Science', 'physical_features': {'color': {'eye': 'blue', 'hair': 'brown'}, 'height': "5'8"} }, 'other': {'favorite_colors': ['purple', 'green', 'blue'], 'interested_in': ['social media', 'intellectual property', 'copyright', 'music', 'books'] } }
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue will call on a linkedlist class class Queue: def __init__(self): self.items = LinkedList() #has to be O(1) def enqueue(self, item): #has to be O(1) def dequeue(self): def is_empty(self): def __len__(self):
In python, why is it advantageous to store not just data (as vectors, arrays,etc.) but perhaps...
In python, why is it advantageous to store not just data (as vectors, arrays,etc.) but perhaps the object (an instance of your user-defined class) that contains and is able to manipulate these data in a data file through maybe the pickle module? Without importing your own class definition, will the data load from such a data file be usable or interpretable? Explain and write some codes to support your ideas if possible.
coding the following project: Setting up structures to store and retrieve data. A major requirement of...
coding the following project: Setting up structures to store and retrieve data. A major requirement of virtually all projects in the financial world involves the storage and retrieval of data. project must be properly modularized and allow for more than one way to store the data to satisfy the various needs of clients.The first manner is by storing data using hashtables (or hash maps) as the basis of storing and retrieving data.
Answer using Jupyter Python #Prompt the user to enter a passing grade and store the value...
Answer using Jupyter Python #Prompt the user to enter a passing grade and store the value from #the input() into a variable. #Refer to the variable in problem 2 to complete the following: #Use the if statement to evaluate the only the first element in the #grades list using index notation. #If the grade is passing, display PASSED, otherwise display FAILED. """Problem 1d""" #Use the for statement to iterate on each element in the #grades list (from problem 1a) and...
Answer using Jupyter Python #Prompt the user to enter a passing grade and store the value...
Answer using Jupyter Python #Prompt the user to enter a passing grade and store the value from #the input() into a variable. #Refer to the variable in problem 2 to complete the following: #Use the if statement to evaluate the only the first element in the #grades list using index notation. #If the grade is passing, display PASSED, otherwise display FAILED. """Problem 1d""" #Use the for statement to iterate on each element in the #grades list (from problem 1a) and...
Utilize Python to create a key-value program similar to MapReduce on Hadoop. For this assignment, do...
Utilize Python to create a key-value program similar to MapReduce on Hadoop. For this assignment, do the following: Create a dictionary (also called an associative array) that contains at least 50 values. An example of a key may be state and value as capital. Another example may be number in an alphabet and letter in an alphabet. Write a command that enumerates the contents of key-values in the dictionary. Write a command that lists all keys. Write a command that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT