3.10.1 Hacks

# Popcorn Hack #1 Python
def shopping_list():
    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

        a_list.append(user_input)
        print("Current shopping list:", a_list)
    
    print("Your final shopping list is:")
    for item in a_list:
        print(item)

shopping_list()
Current shopping list: ['rayhaan sheeraj']
Current shopping list: ['rayhaan sheeraj', 'anvay vahia']
Current shopping list: ['rayhaan sheeraj', 'anvay vahia', 'yuva bala']
Current shopping list: ['rayhaan sheeraj', 'anvay vahia', 'yuva bala', 'kiruthic selvakumar']
Your final shopping list is:
rayhaan sheeraj
anvay vahia
yuva bala
kiruthic selvakumar
// Popcorn Hack #1 JavaScript
function shoppingList() {
    let aList = [];

    while (true) {
        let userInput = prompt("Enter an item you want to add to the shopping list (or 'q' to quit): ");

        if (userInput === 'q') {
            break;
        }

        aList.push(userInput);
        console.log("Current shopping list:", aList);
    }

    console.log("Your final shopping list is:");
    aList.forEach(item => console.log(item));
}

shoppingList();

3.10.2 Hacks

# Popcorn Hack #2 Python

a_list = []

while True:
    user_input = input("Enter an item to add to the list (or 'q' to quit): ")
    if user_input == 'q':
        break
    a_list.append(user_input)

if len(a_list) > 1:
    print("The second element in the list is:", a_list[1])
else:
    print("The list does not have a second element.")

if len(a_list) > 1:
    del a_list[1]
    print("The updated list after deleting the second element is:", a_list)
else:
    print("The list does not have a second element to delete.")
The second element in the list is: bye
The updated list after deleting the second element is: ['hi']
// Popcorn Hack #2 JavaScript
let aList = [];

while (true) {
    let userInput = prompt("Enter an item to add to the list (or 'q' to quit): ");
    if (userInput === 'q') {
        break;
    }
    aList.push(userInput);
}

if (aList.length > 1) {
    console.log("The second element in the list is:", aList[1]);
} else {
    console.log("The list does not have a second element.");
}

if (aList.length > 1) {
    aList.splice(1, 1);
    console.log("The updated list after deleting the second element is:", aList);
} else {
    console.log("The list does not have a second element to delete.");
}

3.10.3 Hacks

// Popcorn Hack #3 JavaScript
%%js

let Foods = ["Water", "Ice Water", "Cold Water", "Hot Water", "Warm Water"];
Foods.push("More Water");
Foods.push("Even More Water");

console.log("List:", Foods);
console.log("Length of List:", Foods.length);
  Cell In[11], line 1
    // Popcorn Hack #3
    ^
SyntaxError: invalid syntax
# Popcorn Hack #3 Python

foods = ["Water", "Ice Water", "Cold Water", "Hot Water", "Warm Water"]
foods.append("More Water")
foods.append("Even More Water")

print("List:", foods)
print("Length of List:", len(foods))
List: ['Water', 'Ice Water', 'Cold Water', 'Hot Water', 'Warm Water', 'More Water', 'Even More Water']
Length of List: 7

3.10.4 Hacks

# Popcorn Hack #4 Python
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sum_value = 0

for num in nums:
    if num % 2 == 0:
        sum_value += num

print("Sum of all even integers in the list:", sum_value)
Sum of all even integers in the list: 30
// Popcorn Hack #4 JavaScript
%% js
let aList = [];

while (true) {
    let userInput = prompt("Enter an item to add to the list (or 'q' to quit): ");
    if (userInput === 'q') {
        break;
    }
    aList.push(userInput);
}

if (aList.length > 1) {
    console.log("The second element in the list is:", aList[1]);
} else {
    console.log("The list does not have a second element.");
}

if (aList.length > 1) {
    aList.splice(1, 1);
    console.log("The updated list after deleting the second element is:", aList);
} else {
    console.log("The list does not have a second element to delete.");
}
  Cell In[2], line 1
    // Popcorn Hack #4 JavaScript
    ^
SyntaxError: invalid syntax
// Popcorn Hack #5 JavaScript
%%js
let fruits = ["apple", "banana", "orange"];

if (fruits.includes("banana")) {
    console.log("banana is in the list");
}
  Cell In[3], line 3
    let fruits = ["apple", "banana", "orange"];
        ^
SyntaxError: invalid syntax
# Popcorn Hack #5 Python

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

if "banana" in fruits:
    print("banana is in the list")
banana is in the list
# Homework #1 Python
numbers = [10, 20, 30, 40, 50]
print("The second element in the list is:", numbers[1])
The second element in the list is: 20
// Homework #2 Javascript
let numbers = [10, 20, 30, 40, 50];
console.log("The second element in the list is:", numbers[1]);
  Cell In[15], line 1
    // Homework #2 Javascript
    ^
SyntaxError: invalid syntax
# Homework #3 Python
def display_menu():
    print("\nTo-Do List Menu:")
    print("1. View To-Do List")
    print("2. Add Item")
    print("3. Remove Item")
    print("4. Quit")

def view_list(todo_list):
    if not todo_list:
        print("Your to-do list is empty.")
    else:
        print("Your to-do list:")
        for index, item in enumerate(todo_list, start=1):
            print(f"{index}. {item}")

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

def remove_item(todo_list):
    view_list(todo_list)
    if todo_list:
        item_index = input("Enter the number of the item to remove: ")
        if item_index.isdigit() and 1 <= int(item_index) <= len(todo_list):
            removed_item = todo_list.pop(int(item_index) - 1)
            print(f"'{removed_item}' has been removed from your to-do list.")
        else:
            print("Invalid item number.")

def main():
    todo_list = []
    while True:
        display_menu()
        choice = input("Enter your choice: ")
        if choice == '1':
            view_list(todo_list)
        elif choice == '2':
            add_item(todo_list)
        elif choice == '3':
            remove_item(todo_list)
        elif choice == '4':
            print("Goodbye!")
            break
        else:
            print("Invalid choice. Please try again.")

if __name__ == "__main__":
    main()
// Homework #3 Javascript
%% js
let aList = [];

while (true) {
    let userInput = prompt("Enter an item to add to the list (or 'q' to quit): ");
    if (userInput === 'q') {
        break;
    }
    aList.push(userInput);
}

if (aList.length > 1) {
    console.log("The second element in the list is:", aList[1]);
} else {
    console.log("The list does not have a second element.");
}

if (aList.length > 1) {
    aList.splice(1, 1);
    console.log("The updated list after deleting the second element is:", aList);
} else {
    console.log("The list does not have a second element to delete.");
}
// Homework #4 JavaScript
let workouts = [];

function displayMenu() {
    console.log("\nWorkout Tracker Menu:");
    console.log("1. View Workouts");
    console.log("2. Add Workout");
    console.log("3. Quit");
}

function viewWorkouts() {
    if (workouts.length === 0) {
        console.log("No workouts logged.");
    } else {
        console.log("Logged Workouts:");
        workouts.forEach((workout, index) => {
            console.log(`${index + 1}. Type: ${workout.type}, Duration: ${workout.duration} mins, Calories: ${workout.calories} kcal`);
        });
    }
}

function addWorkout() {
    let type = prompt("Enter workout type:");
    let duration = prompt("Enter workout duration (in minutes):");
    let calories = prompt("Enter calories burned:");
    workouts.push({ type, duration, calories });
    console.log("Workout added.");
}

while (true) {
    displayMenu();
    let choice = prompt("Enter your choice:");
    if (choice === '1') {
        viewWorkouts();
    } else if (choice === '2') {
        addWorkout();
    } else if (choice === '3') {
        console.log("bye!");
        break;
    } else {
        console.log("Invalid choice. Please try again.");
    }
}
  Cell In[16], line 1
    // Homework #4 JavaScript
    ^
SyntaxError: invalid syntax
# Homework #4 Python
workouts = []

def display_menu():
    print("\nWorkout Tracker Menu:")
    print("1. View Workouts")
    print("2. Add Workout")
    print("3. Quit")

def view_workouts():
    if len(workouts) == 0:
        print("No workouts logged.")
    else:
        print("Logged Workouts:")
        for index, workout in enumerate(workouts, start=1):
            print(f"{index}. Type: {workout['type']}, Duration: {workout['duration']} mins, Calories: {workout['calories']} kcal")

def add_workout():
    type = input("Enter workout type: ")
    duration = input("Enter workout duration (in minutes): ")
    calories = input("Enter calories burned: ")
    workouts.append({ 'type': type, 'duration': duration, 'calories': calories })
    print("Workout added.")

while True:
    display_menu()
    choice = input("Enter your choice: ")
    if choice == '1':
        view_workouts()
    elif choice == '2':
        add_workout()
    elif choice == '3':
        print("Goodbye!")
        break
    else:
        print("Invalid choice. Please try again.")
Workout Tracker Menu:
1. View Workouts
2. Add Workout
3. Quit
No workouts logged.

Workout Tracker Menu:
1. View Workouts
2. Add Workout
3. Quit
No workouts logged.

Workout Tracker Menu:
1. View Workouts
2. Add Workout
3. Quit
Workout added.

Workout Tracker Menu:
1. View Workouts
2. Add Workout
3. Quit
Logged Workouts:
1. Type: all of them, Duration: 10000 mins, Calories: 10000 kcal

Workout Tracker Menu:
1. View Workouts
2. Add Workout
3. Quit
Goodbye!