97.0k views
5 votes
Assume the variable dct references a dictionary. Write an if statement that determines whether the key 'James' exists in the dictionary. If so, display the value that is associated with that key. If the key is not in the dictionary, display a message indicating so.

User Norris
by
4.9k points

1 Answer

6 votes

Answer:

if "James" in dct :

print("Key Exists")

else:

print("Key does not exist")

Step-by-step explanation:

The following assumptions is made.

The dictionary has already been declared and initialized with the required keys and values

The if condition is as follows:

This checks if key "James" exists in the dictionary

if "James" in dct :

This prints a message that the key exists

print("Key Exists")

If otherwise

else:

This prints a message that the key does not exist

print("Key does not exist")

User Kuromoka
by
5.2k points