Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update q3.py #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/q3.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,27 @@ def update_dictionary(dct, key, value):
# Invoke the function "update_dictionary" using the following scenarios:
# - {}, "name", "Alice"
# - {"age": 25}, "age", 26

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.
"""
# Check if the key already exists
if key in dct:
print(f"Original value for '{key}': {dct[key]}")

# Update the dictionary with the new value
dct[key] = value

# Return the updated dictionary
return dct

Task 2
# Scenario 1: {}, "name", "Alice"
print(update_dictionary({}, "name", "Alice")) # Output: {"name": "Alice"}

# Scenario 2: {"age": 25}, "age", 26
print(update_dictionary({"age": 25}, "age", 26)) # Output: prints "Original value for 'age': 25" and returns {"age": 26}