3.8 Popcorn Hacks and Homework


Popcorn Hack 1

%%javascript
for (let i = 1; i < 5; i++) {
    console.log(i);
}
<IPython.core.display.Javascript object>

Popcorn Hack 2

cars = ['mercedes' , 'bmw' , 'audi']

for car in cars:
    print(car)
mercedes
bmw
audi

Popcorn Hack 3

import random

flip = ""

while flip != "tails":
    flip = random.choice(["heads", "tails"])
    print(f"Flipped: {flip}")

print("Landed on tails!")
Flipped: tails
Landed on tails!

Homework 1 for 3.8

person = {'name': 'Darsh', 'age': 17}

keys = list(person.keys())
for i in range(len(keys)):
    print(keys[i], person[keys[i]])

values = list(person.values())
for i in range(len(values)):
    print(values[i])

name Darsh
age 17
Darsh
17
%%javascript

const person = { name: 'Darsh', age: 17 };

const keys = Object.keys(person);
for (let i = 0; i < keys.length; i++) {
    console.log(keys[i], person[keys[i]]);
}

const values = Object.values(person);
for (let i = 0; i < values.length; i++) {
    console.log(values[i]);
}

<IPython.core.display.Javascript object>

Homework 2 for 3.8


number = 50

while number <= 100:
    output = ""
    
    if number % 4 == 0:  
        output += "Fizz"
    if number % 5 == 0:
        output += "Buzz"
    if number % 7 == 0:
        output += "Boom"
    
    
    if output == "":
        print(number)
    else:
        print(output)
    
    number += 1

Buzz
51
Fizz
53
54
Buzz
FizzBoom
57
58
59
FizzBuzz
61
62
Boom
Fizz
Buzz
66
67
Fizz
69
BuzzBoom
71
Fizz
73
74
Buzz
Fizz
Boom
78
79
FizzBuzz
81
82
83
FizzBoom
Buzz
86
87
Fizz
89
Buzz
Boom
Fizz
93
94
Buzz
Fizz
97
Boom
99
FizzBuzz

correct_username = "user123"
correct_password = "pass123"
max_attempts = 3


def user_login():
    for attempts in range(max_attempts):
        username = input("Enter username: ")
        password = input("Enter password: ")
        if username == correct_username and password == correct_password:
            print("Login successful!")
            return True
        print(f"Incorrect. {max_attempts - attempts - 1} attempts left.")
    print("Account is locked.")
    return False


def reset_password():
    if input("Security question: First pet's name? ") == "your_pet_name":
        new_password = input("Enter new password: ")
        global correct_password
        correct_password = new_password
        print("Password reset successfully!")
    else:
        print("Incorrect answer.")

# Main process
if not user_login():
    reset_password()

Login successful!

Homework 3 for 3.8


tasks = [
    "Complete math homework",
    "Write essay for English class",
    "Review science notes",
    "Go for a run",
    "Read a book",
    "Clean the room"
]

def display_tasks(task_list):
    print("Your Tasks:")
    for index, task in enumerate(task_list):
        print(f"{index + 1}. {task}")


display_tasks(tasks)

Your Tasks:
1. Complete math homework
2. Write essay for English class
3. Review science notes
4. Go for a run
5. Read a book
6. Clean the room

Homework 4 for 3.8

%%javascript

for (let i = 0; i < 10; i += 2) {
    if (i === 8) {
        break;  // Stop the loop when i reaches 8
    }
    console.log(i);
}

<IPython.core.display.Javascript object>
for num in range(10):
    if num % 2 == 0:
        continue  
    print(f"This number is... {num}")

This number is... 1
This number is... 3
This number is... 5
This number is... 7
This number is... 9

Popcorn Hack 1 for 3.8.4

%%javascript

for (let i = 1; i <= 15; i += 4) {
    if (i > 10) {
        break;  // Stop the loop when i exceeds 10
    }
    console.log("Current value:", i);
}

<IPython.core.display.Javascript object>

Popcorn Hack 2 for 3.8.4

for num in range(10):
    if num % 2 == 0:
        continue  
    print(f"This number is... {num}")

This number is... 1
This number is... 3
This number is... 5
This number is... 7
This number is... 9