Popcorn Hacks
%%js
//Popcorn hack 1
// Step 1: Initialize an empty array to hold the numbers
let numbers = [];
// Step 2: Use a for loop to count from 0 to 4
for (let i = 0; i < 5; i++) {
numbers.push(i); // Add the current number to
<IPython.core.display.Javascript object>
# Popcorn Hack 2
# Step 1: Create a list of fruits
fruits = ["Apple", "Banana", "Cherry", "Mango", "Orange"]
# Step 2: Display the list of fruits
print("My favorite fruits are:")
for fruit in fruits:
print(fruit)
My favorite fruits are:
Apple
Banana
Cherry
Mango
Orange
%%js
// Popcorn Hack 3
// Step 1: Use a for loop to iterate through numbers from 1 to 20
console.log("Even numbers between 1 and 20:");
for (let i = 1; i <= 20; i++) {
// Step 2: Check if the number is even
if (i % 2 === 0) {
console.log(i); // Step 3: Print the even number
}
}
#Popcorn hack 4
import random # Import the random module
def flip_coin():
"""Simulate flipping a coin."""
return random.choice(['heads', 'tails']) # Randomly choose 'heads' or 'tails'
def simulate_coin_flips():
"""Simulate flipping the coin until it lands on heads."""
flip_count = 0 # Initialize the flip count
while True: # Loop until we get heads
flip_result = flip_coin() # Flip the coin
flip_count += 1 # Increment the flip count
print(f"Flip {flip_count}: {flip_result}") # Print the result of the flip
if flip_result == 'heads': # Check if the result is heads
print(f"It took {flip_count} flips to get heads!") # Print the total flips
break # Exit the loop if heads is obtained
# Call the function to simulate coin flips
simulate_coin_flips()
Flip 1: tails
Flip 2: tails
Flip 3: heads
It took 3 flips to get heads!
# Popcorn hack 5
# List of tasks
tasks = [
"Finish homework",
"Clean the room",
"Go grocery shopping",
"Read a book",
"Exercise"
]
# Function to display tasks with indices
def display_tasks():
print("Your To-Do List:")
for index in range(len(tasks)):
print(f"{index + 1}. {tasks[index]}") # Display task with its index
# Call the function
display_tasks()
%% js
// Popcorn hack 6
// List of tasks
const tasks = [
"Finish homework",
"Clean the room",
"Go grocery shopping",
"Read a book",
"Exercise"
];
// Function to display tasks with indices
function displayTasks() {
console.log("Your To-Do List:");
for (let index = 0; index < tasks.length; index++) {
console.log(`${index + 1}. ${tasks[index]}`); // Display task with its index
}
}
// Call the function
displayTasks();
%%js
// Popcorn hack 7
// Step 1: Initialize a variable to keep track of the count
let count = 1;
// Step 2: Use a while loop to count from 1 to 10
while (count <= 10) {
console.log(`Current count: ${count}`); // Print the current count
// Step 3: Check if the count is 5
if (count === 5) {
console.log("Count has reached 5, breaking the loop."); // Inform about breaking
break; // Step 4: Break out of the loop when count is 5
}
count++; // Increment the count
}
// Step 5: Print a message after the loop
console.log("Loop has ended.");
# Popcorn hack 8
# Step 1: Use a for loop to iterate through numbers from 1 to 10
for number in range(1, 11):
# Step 2: Check if the current number is 5
if number == 5:
continue # Step 3: Skip the rest of the loop when number is 5
# Step 4: Print the current number
print(f"Current number: {number}")
# Step 5: Print a message after the loop
print("Loop has finished.")
Homework Hacks
#Homework hack 1
# Step 1: Create a dictionary of favorite hobbies
hobbies = {
"1": "Reading",
"2": "Biking",
"3": "Gym",
"4": "Hiking",
"5": "Cooking"
}
# Step 2: Loop through the dictionary and print each person's favorite hobby
print("Favorite Hobbies:")
for number, hobby in hobbies.items():
print(f"{number}'s favorite hobby is {hobby}.")
# Step 3: Add a second loop to count from 1 to 5
print("\nCounting from 1 to 5:")
for number in range(1, 6):
print(number)
#Homework hack 2
# Step 1: Initialize the starting number
number = 50
# Step 2: Use a while loop to iterate from 50 to 100
while number <= 100:
output = "" # Initialize an empty output string
# Step 3: Check the conditions for Fizz, Buzz, and Boom
if number % 4 == 0: # Modify condition for multiples of 4
output += "Fizz"
if number % 5 == 0:
output += "Buzz"
if number % 7 == 0:
output += "Boom"
# Step 4: If output is empty, use the number itself
if output == "":
output = number
# Step 5: Print the output for the current number
print(output)
# Step 6: Increment the number
number += 1
#Homework hack 3
# Step 1: Set up credentials and security question
correct_username = "user123"
correct_password = "pass123"
security_question = "What is your favorite color?"
correct_answer = "blue"
# Step 2: Initialize attempts
attempts = 3
authenticated = False
# Step 3: Start the login process using a do-while loop structure
while attempts > 0:
# Prompt user for credentials
username = input("Enter your username: ")
password = input("Enter your password: ")
# Check if credentials are correct
if username == correct_username and password == correct_password:
authenticated = True
break # Exit the loop if authenticated
else:
attempts -= 1 # Reduce the number of attempts
print(f"Incorrect username or password. You have {attempts} attempts left.")
# Step 4: Check if authenticated
if authenticated:
print("Login successful! Welcome!")
else:
print("Account locked due to too many failed attempts.")
# Step 5: Reset password functionality
answer = input(f"To reset your password, answer this security question: {security_question} ")
if answer.lower() == correct_answer:
new_password = input("Enter your new password: ")
correct_password = new_password # Reset the password
print("Password reset successful! You can now log in with your new password.")
else:
print("Incorrect answer. Password reset failed.")
#Homework hack 4
# Step 1: Define Your Tasks
tasks = [
"Buy groceries",
"Clean the house",
"Finish homework",
"Read a book",
"Exercise for 30 minutes",
"Call a friend",
"Prepare dinner",
"Do laundry",
"Walk the dog",
"Organize workspace"
]
# Step 2: Create a Display Function
def display_tasks(task_list):
print("To-Do List:")
for index, task in enumerate(task_list):
print(f"{index + 1}. {task}")
# Step 3: Call the Function
display_tasks(tasks)
%% js
// Homework hack 5
// Define the maximum value
const maxNumber = 20;
// Initialize the number
let number = 0;
while (true) { // Infinite loop
// Print the current number
console.log(number);
// Increment the number by 2
number += 2;
// Check if the number exceeds the maximum value
if (number > maxNumber) {
break; // Exit the loop if the number exceeds the max value
}
}
#Homework hack 6
# Define the maximum value
max_number = 10
# Loop through numbers from 0 to max_number
for number in range(max_number + 1):
# Skip the number 5
if number == 5:
continue
# Print the current number with a message
print(f"This number is: {number}")