❓ Help DICTIONARY PYTHON CODE

PHC-kT

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."
 

About this Thread

  • 1
    Replies
  • 321
    Views
  • 1
    Participants
Last reply from:
Unknown user

Online now

Members online
421
Guests online
7,761
Total visitors
8,182

Forum statistics

Threads
2,338,659
Posts
29,304,811
Members
1,124,504
Latest member
Johncena0301
Back
Top