Popcorn Hack 1

def shopping_list():
    # Create an empty list to store items
    a_list = []

    while True:
        user_input = input("Enter an item you want to add to the shopping list (or 'q' to quit): ")

        if user_input == 'q':
            break

        shopping_list = a_list.append(user_input)

        print("Your shopping list is:" + str(a_list))
    
    # Final display of the shopping list
    print("Your final shopping list is:")
    
    for item in a_list:
        print(item)

shopping_list()

Your shopping list is:['apple']
Your shopping list is:['apple', 'banana']
Your shopping list is:['apple', 'banana', 'fig']
Your shopping list is:['apple', 'banana', 'fig', 'flour']
Your shopping list is:['apple', 'banana', 'fig', 'flour', 'eggs']
Your final shopping list is:
apple
banana
fig
flour
eggs

Popcorn Hack 2

# Create an empty list
aList = []

# Input items into the list
while True:
    item = input("Enter an item to add to the list (or type 'done' to finish): ")
    if item.lower() == 'done':
        break
    aList.append(item)

# Display the second element if it exists
if len(aList) > 1:
    print(f"The second element is: {aList[1]}")
else:
    print("The list does not have a second element.")

# Delete the second element if it exists
if len(aList) > 1:
    del aList[1]
    print("The second element has been deleted.")
else:
    print("There was no second element to delete.")

# Display the updated list
print("The updated list is:", aList)
The second element is: pencils
The second element has been deleted.
The updated list is: ['paper', 'eraser', 'pen', 'pencil case', 'binder']

Popcorn Hack 3

%%js 

// Create a list of five favorite foods
let favoriteFoods = ["Pizza", "Corn", "Burgers", "Pasta", "Ice Cream"];

// Add two more items to the list using the .push() method
favoriteFoods.push("Cake");
favoriteFoods.push("Chocolate");

// Find and print the total number of items in the list using .length
let totalItems = favoriteFoods.length;
console.log("Total number of items in the list:", totalItems);
<IPython.core.display.Javascript object>

Popcorn Hack 4

# Define the list of integers
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Define a variable to represent the sum of even numbers
sum_even = 0

# Iterate through the list and add even numbers to sum_even
for num in nums:
    if num % 2 == 0:  # Check if the number is even
        sum_even += num  # Add the even number to sum_even

# Print the sum of all even numbers in the list
print("Sum of all even numbers in the list:", sum_even)
Sum of all even numbers in the list: 30

Popcorn Hack 5

%%js
let fruits = ["apple", "banana", "orange"];

// Check if "banana" is in the list
if (fruits.includes("banana")) {
    console.log("Banana is in the list at index", fruits.indexOf("banana"));
} else {
    console.log("Banana is not in the list.");
}
<IPython.core.display.Javascript object>

Homework Hacks

Hack #1 Numbers List (Python)

# Create a list of numbers
numbers = [10, 20, 30, 40, 50]

# Print the second element
print(numbers[1])
20

Hack #2 Numbers List (JS)

%%js 

let numbers = [10, 20, 30, 40, 50];

console.log("The second element in the array is:", numbers[1]);
<IPython.core.display.Javascript object>

Hack #3 For loops with lists

fruitList = ["apple", "banana", "cherry", "date", "fig", "grape"] # Create a list of fruits

for fruit in fruitList: # Loop through each fruit in the list
    print(fruit) # Print the fruit
apple
banana
cherry
date
fig
grape

Hack #4 Todo-List Python

# Create an empty list to store the to-do items
to_do_list = []

# Define a function to add an item to the to-do list
def add_item(item):
    to_do_list.append(item)
    print(f"Added '{item}' to the to-do list.")

# Define a function to remove an item from the to-do list
def remove_item(item):
    if item in to_do_list:
        to_do_list.remove(item)
        print(f"Removed '{item}' from the to-do list.")
    else:
        print(f"'{item}' is not in the to-do list.")

# Define a function to view the to-do list
def view_list():
    print("To-Do List:")
    for item in to_do_list:
        print(item)

# Main program loop
while True:
    action = input("Enter 'add' to add an item, 'remove' to remove an item, 'view' to view the list, or 'quit' to exit: ")
    
    if action == 'add':
        item = input("Enter the item to add: ")
        add_item(item)
    elif action == 'remove':
        item = input("Enter the item to remove: ")
        remove_item(item)
    elif action == 'view':
        view_list()
    elif action == 'quit':
        break
    else:
        print("Invalid action. Please try again.")
Invalid action. Please try again.
Added 'math hw' to the to-do list.
Added 'essay' to the to-do list.
Added 'CSP' to the to-do list.
Added 'chem' to the to-do list.
Removed 'math hw' from the to-do list.
To-Do List:
essay
CSP
chem

Hack #5 Workout Tracker JS

%%js

// Create an empty list to store the workouts
let workoutList = [];

// Function to add a workout
function addWorkout(type, duration, calories) {
    let workout = {
        type: type,
        duration: duration,
        calories: calories
    };
    workoutList.push(workout);
    console.log(`Added workout: ${type}, Duration: ${duration} minutes, Calories: ${calories} kcal.`);
}

// Function to remove a workout by its index
function removeWorkout(index) {
    if (index >= 0 && index < workoutList.length) {
        const removed = workoutList.splice(index, 1);
        console.log(`Removed workout: ${removed[0].type}`);
    } else {
        console.log("Invalid index. Unable to remove workout.");
    }
}

// Function to view the workout list
function viewWorkouts() {
    console.log("Workout List:");
    workoutList.forEach((workout, index) => {
        console.log(`${index + 1}: Type: ${workout.type}, Duration: ${workout.duration} minutes, Calories: ${workout.calories} kcal.`);
    });
}

// Main program loop
while (true) {
    let action = prompt("Enter 'add' to log a workout, 'remove' to delete a workout, 'view' to see workouts, or 'quit' to exit:");
    
    if (action === 'add') {
        let type = prompt("Enter workout type:");
        let duration = prompt("Enter duration in minutes:");
        let calories = prompt("Enter calories burned:");
        addWorkout(type, duration, calories);
    } else if (action === 'remove') {
        let index = prompt("Enter the workout number to remove:") - 1;
        removeWorkout(index);
    } else if (action === 'view') {
        viewWorkouts();
    } else if (action === 'quit') {
        break;
    } else {
        console.log("Invalid action. Please try again.");
    }
}