-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ3.py
More file actions
30 lines (19 loc) · 641 Bytes
/
Q3.py
File metadata and controls
30 lines (19 loc) · 641 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def update_dictionary(dct, key, value):
"""
Task 1
- Create a function that updates a dictionary (dct) with a new key-value pair.
- If the key already exists in dct, print the original value, then update its value.
- Return the updated dictionary.
"""
# Validate that dct is a dictionary
if not isinstance(dct, dict):
return -1
# If key exists, print original value
if key in dct:
print(dct[key])
# Update the dictionary
dct[key] = value
return dct
# Task 2 Examples
print(update_dictionary({}, "name", "Alice"))
print(update_dictionary({"age": 25}, "age", 26))