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.
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'] } }
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...
Write a python program that keeps names and email addresses in a dictionary as key-value pairs....
Write a python program that keeps names and email addresses in a dictionary as key-value pairs. The program should display a menu that lets the user look up a person’s email address, add a new name and email address, change an existing email address, and delete an existing name and email address. The program should bind the dictionary and save it to a file when the user exits the program. Each time the program starts, it should retrieve the dictionary...
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value...
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value pairs of the dictionary as tuples (key, value), reverse sorted by value (highest first) and where multiple keys with the same value appear alphabetically (lowest first).
EBIT—EPS and capital structure  Data-Check is considering two capital structures. The key information is shown in...
EBIT—EPS and capital structure  Data-Check is considering two capital structures. The key information is shown in the following table. Assume a 21% tax rate. Source of Capital Structure A Structure B Long Term Debt $94,000 at 15.3% coupon rate $188,000 at 16.3% coupon rate Common Stock 4,300 shares 2,150 shares a. Calculate two ​EBIT-EPS coordinates for each of the structures by selecting $50,000 & $60,000 EBIT values and finding their associated EPS values. b. Plot the two capital structures on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT