What's new

Help DICTIONARY PYTHON CODE

PHC_Mikasa

Forum Guru
Elite
Create a program with a predefined dictionary of a person. Include the following information: name, gender, age, address and phone.

Ask the user what information he wants to know about the person (example: "name"), then print the value associated to that key or display a message in case the key is not found.
 
Sure! Here's a Python program that uses a predefined dictionary to store information about a person and allows the user to query specific details:

Python:
person = {
    "name": "John Doe",
    "gender": "Male",
    "age": 30,
    "address": "123 Main Street, City",
    "phone": "123-456-7890"
}

key = input("What information would you like to know about the person? ")

if key in person:
    value = person[key]
    print(f"{key.capitalize()}: {value}")
else:
    print("Key not found in the dictionary.")

In this program, we first define the person dictionary containing various information about a person. Then, we prompt the user to input the key (e.g., "name", "gender", etc.) for the information they want to know.

The program checks if the key exists in the person dictionary using the in operator. If the key is found, it retrieves the corresponding value and displays it. If the key is not found, it prints a message stating that the key was not found in the dictionary.

You can run this program and enter different keys to retrieve information about the person. For example, if you enter "name" as the key, it will output "Name: John Doe". If you enter an invalid key, it will display "Key not found in the dictionary."
 

Similar threads

Back
Top