def sum_of_two(num1, num2):
"""This function takes two numbers and returns their sum."""
return num1 + num2
attempt to get user input
try:
ask the user for two integer inputs
num_one = int(input("Please enter the first number: "))
num_two = int(input("Please enter the second number: "))
# call the function to calculate the sum
result = sum_of_two(num_one, num_two)
# display the result of the addition
print(f"The result of adding {num_one} and {num_two} is: {result}")
handle invalid input
except ValueError:
print("Error: Please make sure you enter valid numbers.")
def update_info(key, new_value):
if key in employee:
employee[key] = new_value # update if key exists
print(f"{key} updated to {new_value}")
else:
print(f"'{key}' doesn’t exist in the dictionary")
updating age to 31
update_info("age", 31)
updating position to something else
update_info("position", "Director") # this will work
update_info("department", "HR") # this won’t work, 'department' isn't in dict
print updated dict
print("updated dictionary:", employee)
simple function to print employee info summary
def summary():
print("\nemployee summary:")
for key, value in employee.items():
print(f"{key}: {value}")
data types part 1
function that adds two numbers
def sum_of_two(num1, num2): """This function takes two numbers and returns their sum.""" return num1 + num2
attempt to get user input
try:
ask the user for two integer inputs
handle invalid input
except ValueError: print("Error: Please make sure you enter valid numbers.")
data types part 2
creating a dictionary with a person's details
individual = { "first_name": "John", "age": 30, "is_enrolled": True }
updating the age value in the dictionary
individual["age"] = 31 # updates John's age
adding another person's details
individual["Sarah"] = { "first_name": "Sarah", "age": 28, "is_enrolled": False }
create a dictionary with 3 keys, print it
employee = { "name": "John", "age": 45, "position": "Manager" } print("original dictionary:", employee)
function to update any value in the dict
def update_info(key, new_value): if key in employee: employee[key] = new_value # update if key exists print(f"{key} updated to {new_value}") else: print(f"'{key}' doesn’t exist in the dictionary")
updating age to 31
update_info("age", 31)
updating position to something else
update_info("position", "Director") # this will work update_info("department", "HR") # this won’t work, 'department' isn't in dict
print updated dict
print("updated dictionary:", employee)
simple function to print employee info summary
def summary(): print("\nemployee summary:") for key, value in employee.items(): print(f"{key}: {value}")
call summary to display final employee details
summary()