Popcorn and Homework Hacks

# 10.1 Popcorn Hack 1

a_list = []
user_input = None

# Simulate user inputs (you can replace these with real inputs)
user_input = "apple"
a_list.append(user_input)

user_input = "banana"
a_list.append(user_input)

user_input = "strawberry"
a_list.append(user_input)

# Use a for loop to print each item in the list
for item in a_list:
    print(item)

apple
banana
strawberry
%%javascript

/* 10.1 Popcorn Hack 1 Javascript*/

let a_list = [];
let user_input;

// Simulate user inputs (you can replace these with real inputs, e.g., from a form or prompt)
user_input = "apple";
a_list.push(user_input);

user_input = "banana";
a_list.push(user_input);

user_input = "strawberry";
a_list.push(user_input);

// Use a for loop to print each item in the list
for (let i = 0; i < a_list.length; i++) {
    console.log(a_list[i]);
}

<IPython.core.display.Javascript object>
# 10.2 Popcorn Hack

def secondElementfunc(aList):
    if len(aList) >= 2:
        print(f"Second element: {aList[1]}")
        del aList[1]
        print(f"Updated list after deleting second element: {aList}")
    else:
        print("There is no second element in the list.")

# Example usage
aList = ["book", "pencil", "pen", "computer"]
secondElementfunc(aList)

Second element: pencil
Updated list after deleting second element: ['book', 'pen', 'computer']
%%javascript

/* 10.2 Popcorn Hack Javascript */

function secondElementfunc(aList) {
    if (aList.length >= 2) {
        console.log("Second element:", aList[1]);
        aList.splice(1, 1);  // Delete the second element
        console.log("Updated list after deleting second element:", aList);
    } else {
        console.log("There is no second element in the list.");
    }
}

// Example usage
let aList = ["book", "pencil", "pen", "computer"];
secondElementfunc(aList);
<IPython.core.display.Javascript object>
# 10.3 Popcorn Hack

# Create a list of favorite foods
favorite_foods = ["pizza", "quesadilla", "ice cream", "pasta", "tacos"]

# Add two more items to the list
favorite_foods.append("samosa")
favorite_foods.append("cookie")

# Print the list
print("Favorite foods:", favorite_foods)

# Find and print the total number of items in the list
print("Total number of items:", len(favorite_foods))

Favorite foods: ['pizza', 'quesadilla', 'ice cream', 'pasta', 'tacos', 'samosa', 'cookie']
Total number of items: 7
%%javascript

/* 10.3 Popcorn Hack Javascript*/

let favorite_foods = ["pizza", "quesadilla", "ice cream", "pasta", "tacos"];

// Add two more items to the list
favorite_foods.push("samosa");
favorite_foods.push("cookie");

// Print the list
console.log("Favorite foods:", favorite_foods);

// Find and print the total number of items in the list
console.log("Total number of items:", favorite_foods.length);
# 10.4 Python Popcorn Hack
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

sum_evens = 0

for num in nums:
    if num % 2 == 0:  # Check if the number is even
        sum_evens += num
print("Sum of even numbers:", sum_evens)

Sum of even numbers: 30
%%javascript

let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

let sum_evens = 0;

for (let num of nums) {
    if (num % 2 === 0) {  // Check if the number is even
        sum_evens += num;
    }
}

console.log("Sum of even numbers:", sum_evens);

<IPython.core.display.Javascript object>
# 10.4 Python Popcorn Hack

fruits = ["apple", "orange", "banana", "grape", "pear"]

if "banana" in fruits:
    print("Banana is in the list.")
else:
    print("Banana is not in the list.")
Banana is in the list.
%%javascript

/* 10.4 Popcorn Hack Javascript */

let fruits = ["apple", "orange", "banana", "grape", "pear"];

if (fruits.includes("banana")) {
    console.log("Banana is in the list.");
} else {
    console.log("Banana is not in the list.");
}
<IPython.core.display.Javascript object>
# Homework Hack 1

nums = [10, 20, 30, 40, 50]

print("Second element:", nums[1])

Second element: 20
%%javascript

/* Homework Hack 2 in Javascript */
 
let numbers = [10, 20, 30, 40, 50];
console.log("Second element:", numbers[1]);

<IPython.core.display.Javascript object>
#Homework Hack 3 

def display_menu():
    print("\nTo-Do List Menu:")
    print("1. Add an item")
    print("2. Remove an item")
    print("3. View items")
    print("4. Exit")

def main():
    todo_list = []

    while True:
        display_menu()
        choice = input("Choose an option (1-4): ")

        if choice == '1':
            item = input("Enter the item to add: ")
            todo_list.append(item)
            print(f"'{item}' has been added to your to-do list.")

        elif choice == '2':
            item = input("Enter the item to remove: ")
            if item in todo_list:
                todo_list.remove(item)
                print(f"'{item}' has been removed from your to-do list.")
            else:
                print(f"'{item}' not found in your to-do list.")

        elif choice == '3':
            if todo_list:
                print("\nYour To-Do List:")
                for idx, item in enumerate(todo_list, start=1):
                    print(f"{idx}. {item}")
            else:
                print("Your to-do list is empty.")

        elif choice == '4':
            print("Exiting the to-do list program. Goodbye!")
            break

        else:
            print("Invalid option. Please choose a valid option (1-4).")

if __name__ == "__main__":
    main()

To-Do List Menu:
1. Add an item
2. Remove an item
3. View items
4. Exit
'homework' has been added to your to-do list.

To-Do List Menu:
1. Add an item
2. Remove an item
3. View items
4. Exit

Your To-Do List:
1. homework

To-Do List Menu:
1. Add an item
2. Remove an item
3. View items
4. Exit
'homework' has been removed from your to-do list.

To-Do List Menu:
1. Add an item
2. Remove an item
3. View items
4. Exit
Exiting the to-do list program. Goodbye!
%%javascript

// Function to display the menu
function displayMenu() {
    console.log("\nWorkout Tracker Menu:");
    console.log("1. Log a workout");
    console.log("2. View workouts");
    console.log("3. Exit");
}

// Main function
function main() {
    const workouts = [];
    let running = true;

    while (running) {
        displayMenu();
        const choice = prompt("Choose an option (1-3):");

        switch (choice) {
            case '1': // Log a workout
                const type = prompt("Enter the type of workout (e.g., Running, Cycling):");
                const duration = prompt("Enter the duration of the workout (in minutes):");
                const calories = prompt("Enter the calories burned:");

                const workout = {
                    type: type,
                    duration: parseInt(duration),
                    calories: parseInt(calories)
                };

                workouts.push(workout);
                console.log(`Logged: ${type} for ${duration} minutes, burning ${calories} calories.`);
                break;

            case '2': // View workouts
                if (workouts.length > 0) {
                    console.log("\nYour Workouts:");
                    workouts.forEach((workout, index) => {
                        console.log(`${index + 1}. Type: ${workout.type}, Duration: ${workout.duration} minutes, Calories: ${workout.calories}`);
                    });
                } else {
                    console.log("No workouts logged yet.");
                }
                break;

            case '3': // Exit
                console.log("Exiting the workout tracker. Goodbye!");
                running = false;
                break;

            default:
                console.log("Invalid option. Please choose a valid option (1-3).");
                break;
        }
    }
}

// Run the program
main();

<IPython.core.display.Javascript object>