For Loops

Popcorn Hack 1

%%js
for (let i = 10; i > 0; i--) {
    console.log(i);
}

// I changed the numbers to count down instead of count up
<IPython.core.display.Javascript object>

Popcorn Hack 2

colors = ['White', 'Blue', 'Black']

for color in colors:
    print(color)

#I have it to add letters instead 
White
Blue
Black

Homework Hack

Python

# Define a dictionary representing a car
car = {'make': 'Tesla', 'model': 'Model 3', 'year': 2020}

# Looping through keys
for key in car:
    print(f'Key: {key}, Value: {car[key]}')

# Looping through values
for value in car.values():
    print(f'Car Value: {value}')

# Looping through keys and values
for key, value in car.items():
    print(f'Car Key: {key}, Car Value: {value}')
Key: make, Value: Tesla
Key: model, Value: Model 3
Key: year, Value: 2020
Car Value: Tesla
Car Value: Model 3
Car Value: 2020
Car Key: make, Car Value: Tesla
Car Key: model, Car Value: Model 3
Car Key: year, Car Value: 2020

JavaScript

%%js
// Define a dictionary representing a phone
let phone = {'brand': 'Apple', 'model': 'iPhone 13', 'year': 2021};

// Looping through keys
for (let key in phone) {
    console.log(`Key: ${key}, Value: ${phone[key]}`);
}

// Looping through values
Object.values(phone).forEach(value => {
    console.log(`Phone Value: ${value}`);
});

// Looping through keys and values
Object.entries(phone).forEach(([key, value]) => {
    console.log(`Phone Key: ${key}, Phone Value: ${value}`);
});
<IPython.core.display.Javascript object>

While and Do-While Loop

Popcorn Hack 1

number = 1

while number <= 20:
    if number % 2 == 1:
        print(number)
    number += 2

#Printing odd numbers
1
3
5
7
9
11
13
15
17
19

Popcorn Hack 2

import random

roll = ""

while roll != "side 1":
    roll = random.choice(["side 1", "side 2", "side 3", "side 4", "side 5", "side 6"])
    print(f"You rolled onto: {roll}")

print("Rolled onto side 1!")

#Rolling a dice until it lands on side 1, I made this myself
You rolled onto: side 2
You rolled onto: side 6
You rolled onto: side 1
Rolled onto side 1!

Homework Hack 1

# FizzBuzz with a Twist (Multiples of 7 print 'Boom')
i = 1

while i <= 50:
    if i % 3 == 0 and i % 5 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    elif i % 7 == 0:
        print("Boom")
    else:
        print(i)
    i += 1

# Challenge: Modified conditions, range from 50 to 100, multiples of 4 instead of 3
i = 50

while i <= 100:
    if i % 4 == 0 and i % 5 == 0:
        print("FizzBuzz")
    elif i % 4 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    elif i % 7 == 0:
        print("Boom")
    else:
        print(i)
    i += 1
1
2
Fizz
4
Buzz
Fizz
Boom
8
Fizz
Buzz
11
Fizz
13
Boom
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
Boom
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
Boom
Buzz
Buzz
51
Fizz
53
54
Buzz
Fizz
57
58
59
FizzBuzz
61
62
Boom
Fizz
Buzz
66
67
Fizz
69
Buzz
71
Fizz
73
74
Buzz
Fizz
Boom
78
79
FizzBuzz
81
82
83
Fizz
Buzz
86
87
Fizz
89
Buzz
Boom
Fizz
93
94
Buzz
Fizz
97
Boom
99
FizzBuzz

Homework Hack 2

# User Authentication System with 3 attempts and password reset functionality

correct_username = "Yash"
correct_password = "password"
security_question = "What is your favorite color?"
correct_answer = "Blue"

attempts = 3

while attempts > 0:
    username = input("Enter your username: ")
    password = input("Enter your password: ")

    if username == correct_username and password == correct_password:
        print("Login successful!")
        break
    else:
        attempts -= 1
        print(f"Incorrect credentials. {attempts} attempts remaining.")

    if attempts == 0:
        print("Account locked. You've run out of attempts.")
        reset = input("Would you like to reset your password? (yes/no): ")
        if reset == "yes":
            answer = input(security_question)
            if answer == correct_answer:
                new_password = input("Enter your new password: ")
                correct_password = new_password
                attempts = 3
                print("Password is reset, try logging in again!")
            else:
                print("Security question is wrong, you can't reset.")
        else:
            print("Goodbye!")

Index Loops

Popcorn Hack 1 - Python

# List of tasks
tasks = [
    "Study for bio",
    "Play soccer",
    "Finish math homework",
]

# 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()
Your To-Do List:
1. Study for bio
2. study for chem
3. study for math
%%javascript
// List of tasks
const tasks = [
    "Study for bio",
    "Play soccer",
    "Finish math homework",
];

// 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();

<IPython.core.display.Javascript object>

Continue and Break

Popcorn Hack 1

%%javascript
// Custom break logic: stop inner loop when i equals 3 and outer loop when i equals 7
for (let outer = 0; outer < 10; outer++) {
  console.log(`Outer loop iteration: ${outer}`);

  for (let inner = 0; inner < 10; inner++) {
    if (inner === 3) {
        console.log("Inner loop break");
        break
    }
    console.log(`Inner loop iteration: ${inner}`);
  }

  if (outer === 7) {
      console.log("Outer loop break");
      break
  }
}
<IPython.core.display.Javascript object>

Popcorn Hack 2

# Simple continue loop in Python
for i in range(10):
    if i == 5:
        print("Skipping number 5!")
        continue
    print(f"Number: {i}")
Number: 0
Number: 1
Number: 2
Number: 3
Number: 4
Skipping number 5!
Number: 6
Number: 7
Number: 8
Number: 9

Homework Hack 1

%%javascript
for (let i = 0; i < 20; i += 2) {
    if (i === 10) {
        console.log("Breaking the loop at 10!");
        break;
    }
    console.log(i);
}
<IPython.core.display.Javascript object>

Homework Hack 2

for i in range(10):
    if i == 5:
        continue
    print(f"This number is {i}")
#as you can see number 5 is skipped because it isn't printed
This number is 0
This number is 1
This number is 2
This number is 3
This number is 4
This number is 6
This number is 7
This number is 8
This number is 9