Team Teach 3.2 Homework and Popcorn Hacks
Showing how I was able to work and experiment with these.
3.2 TEAM TEACH Monday
Popcorn hacks + Homework
# Abstracting keys and assigning numbers 1, 2, and 3 to them
fruit_dict = {1: "bananas", 2: "apples", 3: "pears"}
# Creating a corresponding dictionary for their values
fruit_values = {"bananas": 1, "apples": 2, "pears": 3}
# Accessing the value using the key (e.g., key 1 for bananas)
key = 1 # You can change this to 2 or 3 for other fruits
# Output the corresponding fruit and its value
fruit = fruit_dict[key] # This gives us the fruit name
value = fruit_values[fruit] # This gives us the value associated with the fruit
print(f"The fruit for key {key} is {fruit} and its value is {value}.")
The fruit for key 1 is bananas and its value is 1.
Popcorn hack 2
# Simple Command-Line Calculator in Python
# Get user input
first_number = float(input("Please Enter the First Number Here: "))
second_number = float(input("Please Enter the Second Number Here: "))
math_function = input("Please Enter the operation (+, -, *, /): ")
# Perform the calculation based on the math_function input
if math_function == "+":
result = first_number + second_number
elif math_function == "-":
result = first_number - second_number
elif math_function == "*":
result = first_number * second_number
elif math_function == "/":
# Check for division by zero
if second_number == 0:
result = "Error: Division by zero is undefined."
else:
result = first_number / second_number
else:
result = "Invalid operation!"
# Output the result
print("Result:", result)
Result: 4.0
Popcorn Hack 3
def repeat_strings(strings, n):
return [string * n for string in strings] # Using list comprehension to repeat each string `n` times
# Test the function
string_list = ["apple", "banana", "orange"]
repeated_strings = repeat_strings(string_list, 2)
print(repeated_strings)
['appleapple', 'bananabanana', 'orangeorange']
Popcorn hack 4
def sets_have_common_elements(set1, set2):
return not set1.isdisjoint(set2)
print(sets_have_common_elements({1, 2, 3}, {3, 4})) # Output: True
print(sets_have_common_elements({1, 2, 3}, {4, 5})) # Output: False
True
False
Homework
Part 1
profile = {
"name": "Alex Rubio",
"age": 15,
"city": "San diego",
"favorite_color": "purple"
}
print(profile)
{'name': 'Alex Rubio', 'age': 15, 'city': 'San diego', 'favorite_color': 'purple'}
Part 2
# Part 2: Create a hobbies list
hobbies = ["reading", "cycling", "coding"]
print(hobbies)
['reading', 'cycling', 'coding']
Part 3
# Part 3: Add hobbies to the profile dictionary
profile["hobbies"] = hobbies
print(profile)
{'name': 'Alex Rubio', 'age': 15, 'city': 'San diego', 'favorite_color': 'purple', 'hobbies': ['reading', 'cycling', 'coding']}
Part 4
# Part 4: Check availability of a hobby
hobby_of_choice = "cycling" # Choose one of the hobbies
has_hobby = True # Example: Set to True if it's available today
# Print availability of the hobby
print(f"Is {hobby_of_choice} available today? {has_hobby}")
Is cycling available today? True
Part 5
# Part 5: Count total number of hobbies
total_hobbies = len(hobbies)
print(f"I have {total_hobbies} hobbies.")
I have 3 hobbies.
Part 6
# Part 6: Create a tuple for favorite hobbies
favorite_hobbies = ("reading", "coding") # Example: Two favorite hobbies
print(favorite_hobbies)
('reading', 'coding')
Part 7
# Part 7: Create a set of skills
skills = {"programming", "public speaking", "graphic design"} # Example skills
print(skills)
{'programming', 'graphic design', 'public speaking'}
Part 8
# Part 8: Create a variable for new skill
new_skill = None # You haven't decided yet
print(new_skill)
None
Part 9
# Part 9: Calculate the total profile cost
# Each hobby costs $5, and each skill costs $10
hobby_cost = 5
skill_cost = 10
total_cost = (total_hobbies * hobby_cost) + (len(skills) * skill_cost)
print(f"Total cost to pursue hobbies and develop skills: ${total_cost:.2f}")
Total cost to pursue hobbies and develop skills: $45.00