3.10
Popcorn Hacks
3.10.1 List Operations and Append
Hack 1
Use a_list.append(user_input) to append each item. Use a for loop to print out each item in the list at the end.
Build a Shopping List with JavaScript. In this exercise, you will create a simple shopping list application that allows users to add items to a list and display them at the end.
%%html
<script>
let shopping_list = ["eggs", "flour", "milk"]
shopping_list.append(user_input)
</script>
3.10.2 Accessing and Deleting Elements
Hack 1
- Create a list/array named aList.
- Input items into the list/array. After the user is done adding items, display the second element (if it exists) in the list/array.
- After adding items to the list/array, delete the second element (if it exists) and display the updated list/array.
%%html
<script>
let aList = [];
while (true) {
input = prompt("Enter an item (quit to finish):");
if (input.toLowerCase() === 'quit') break;
aList.push(input);
}
if (aList.length >= 2) {
console.log("Element 2:", aList[1]);
console.log("Removing", aList[1]);
aList.splice(1, 1);
}
console.log("Updated list:", aList);
</script>
3.10.3 Assigning a Value & Length of A List
Hack 1
- Create a list of your five favorite foods.
- Add two more items to your list using the .push() method.
- Find and print the total number of items in the list using .length
favorite_foods = ["pasta", "nihari", "scrambled eggs", "ice cream", "pasanday"]
print(favorite_foods)
favorite_foods.append("arsenic")
favorite_foods.append("lava")
print("Length: ", len(favorite_foods), favorite_foods)
['pasta', 'nihari', 'scrambled eggs', 'ice cream', 'pasanday']
Length: 7 ['pasta', 'nihari', 'scrambled eggs', 'ice cream', 'pasanday', 'arsenic', 'lava']
3.10.4 Minimum Value and Finding an Element
Hack 1
Find the sum of all the even numbers of a list called nums with integers using the previous list opperations. Define your list and define a variable to represent a potential value.
nums = [2, 3, 4, 10, 14, 23, 67, 94]
even_sum = 0
for number in nums:
if number % 2 == 0:
print(number)
even_sum += number
print("Sum: ", even_sum)
2
4
10
14
94
Sum: 124
Hack 2
Look for the element “banana” in the list “fruits” using If Else Statements
fruits = ["apple", "banana", "orange"]
if "banana" in fruits:
print("Banana is in the list.")
else:
print("Banana is not in the list.")
Banana is in the list.
Hacks
Hack 1
Write a Python program that creates a list of the following numbers: 10, 20, 30, 40, 50. Then, print the second element in the list.
list1 = [10, 20, 30, 40, 50]
print(list1[1])
20
Hack 2
Write a JavaScript program that creates an array of the following numbers: 10, 20, 30, 40, 50. Then, log the second element in the array to the console.
%%html
<script>
array = [10, 20, 30, 40, 50]
console.log(array[1])
</script>
Hack 3
Python: Create a to-do list in whcih users can add, remove, and view items in their list.
to_do = ["laundry", "cook"]
def add_item():
item = input("\nAdd a task: ")
to_do.append(item)
print(f'"{item}" has been added to your to-do list.')
print(to_do)
while True:
add_item()
choice = input("\nDo you want to add another task? (yes/no): ").lower()
if choice == "no":
print("Exiting the to-do list.")
break
"buy groceries" has been added to your to-do list.
['laundry', 'cook', 'buy groceries']
"stop" has been added to your to-do list.
['laundry', 'cook', 'buy groceries', 'stop']
Exiting the to-do list.
Hack 4
JavaScript: Create a workout tracker where users can log their workouts, including type, duration, and calories burned.
%%html
<script>
let workoutLog = [];
function logWorkout() {
let type = prompt("Enter the workout type (e.g., running, swimming, cycling):");
let duration = prompt("Enter the duration (in minutes):");
let calories = prompt("Enter the calories burned:");
workoutLog.push({ type, duration, calories });
console.log(`Workout logged: ${type}, ${duration} mins, ${calories} calories`);
}
logWorkout();
</script>