๐Ÿ”’ Closed DICTIONARY PYTHON CODE

Status
Not open for further replies.

PHC-kT

Forum Expert
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."
 
Status
Not open for further replies.

About this Thread

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

New Topics

Online now

Members online
675
Guests online
14,875
Total visitors
15,550

Forum statistics

Threads
2,362,175
Posts
29,414,631
Members
1,056,865
Latest member
danielban
Back
Top