Skip to the content.

3.10A HW

3.10 HW and Popcorn Hacks

Tri 1 Documentation

# Popcorn Hack 1

a_list = []

while True:
    user_input = input("Enter an item to add to the list. Type done when you are finished: ")
    if user_input.lower() == 'done':
        break
    a_list.append(user_input)

print("Items in the list:")
for item in a_list:
    print(item)
// Javascript Shoping List

let shoping_list = []

while(true) {
    let userInput = prompt("Enter the item you want to add to the list. To quit type 'q'");
    if(userInput === "q") {
        break;
    }
    shoping_list.push(userInput);
    console.log("Item added to the list");
}

console.log("Shoping List: ");
for(let i = 0; i < shoping_list.length; i++) {
    console.log(shoping_list[i]);
}
# Popcorn Hack 2

a_list = []

while True:
    user_input = input("Enter an item to add to the list. Type done when you are finished: ")
    if user_input.lower() == 'done':
        break
    a_list.append(user_input)

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

print("New list:")
for item in a_list:
    print(item)


// Popcorn Hack 3

let favorite_foods = ["pizza", "ice cream", "pasta", "tacos", "chocolate"];
favorite_foods.push("burgers");
favorite_foods.push("cookies");

let amount = favorite_foods.length;
console.log("Amount of favorite foods: " + amount);
for (let i = 0; i < amount; i++) {
    console.log(favorite_foods[i]);
}


# Popcorn hack 3
num_list = [1,2,3,4,5,6,7,8,9,10]
even_sum = 0

for num in num_list:
    if num % 2 == 0:
        even_sum += num
print("The sum of all even numbers in the list is: " + str(even_sum))
// Popcorn Hack 4
let fruits = ["apple", "banana", "orange"];

if (fruits.includes("banana")) {
    console.log("banana is in the list");
}
else {
    console.log("no banana in the list");
}
# Homework Hack 1
num_list = [10, 20, 30, 40, 50]

print(num_list[1])
// Homework Hack 2

let num_list = [10, 20, 30, 40, 50];
console.log(num_list[1]); // 20    
# Homework Hack 3
todo_list = []

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

def remove_item(item):
    if item in todo_list:
        todo_list.remove(item)
        print(f"'{item}' has been removed from the to-do list.")
    else:
        print(f"'{item}' is not in the to-do list.")

def view_list():
    print("To-do list:")
    for item in todo_list:
        print(item)

while True:
    action = input("What would you like to do? (add, remove, view, quit): ").lower()
    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 choose add, remove, view, or quit.")
// Homework Hack 4
let workout_log = [];

function add_workout(type, duration, calories) {
    let workout = {
        type: type,
        duration: duration,
        calories: calories
    };
    workout_log.push(workout);
    console.log("Workout added to the log");
}

function view_workouts() {
    if (workout_log.length === 0) {
        console.log("No workout in the log");
    } else {
        for (let i = 0; i < workout_log.length; i++) {
            console.log("Workout " + (i + 1) + ": " + workout_log[i].type + " - " + workout_log[i].duration + " minutes - " + workout_log[i].calories + " calories");
        }
    }
}

while (true) {
    let action = prompt("What would you like to do? (add, view, quit): ").toLowerCase();
    if (action === "add") {
        let type = prompt("Enter the workout type: ");
        let duration = prompt("Enter the workout duration in minutes: ");
        let calories = prompt("Enter the calories burned: ");
        add_workout(type, duration, calories);
    } else if (action === "view") {
        view_workouts();
    } else if (action === "quit") {
        break;
    } else {
        console.log("Invalid action. Please choose add, view, or quit.");
    }
}