Homeworks and Popcorn Hacks for Lesson 3.8
All homeworks for Lessons
3.8 Popcorn Hacks and Homework
Popcorn Hack 1
%%js
for (let i = 1; i < 10; i++) {
console.log(i);
}
<IPython.core.display.Javascript object>
Popcorn Hack 2
sports = ['baseball', 'soccer', 'basketball']
for sport in sports:
print(sport)
baseball
soccer
basketball
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!
Popcorn Hack 4
number = 1
while number <= 40:
if number % 2 == 0:
print(number)
number += 3
4
10
16
22
28
34
40
Popcorn Hack 5
tasks = [
"sleep",
"eat",
"shower",
"exercise",
"wake up"
]
def display_tasks():
print("Your To-Do List:")
for index in range(len(tasks)):
print(f"{index + 1}. {tasks[index]}")
display_tasks()
Your To-Do List:
1. sleep
2. eat
3. shower
4. exercise
5. wake up
Popcorn Hack 6
%%javascript
const tasks = [
"Do your hw",
"Walk the dog",
"Feed the fish",
"Ride your bike",
"Do the laundry"
];
function displayTasks() {
console.log("Your To-Do List:");
for (let index = 0; index < tasks.length; index++) {
console.log(`${index + 1}. ${tasks[index]}`);
}
}
displayTasks();
<IPython.core.display.Javascript object>
Popcorn Hack 7
%%javascript
for (let i = 0; i < 20; i++)
for (let i = 0; i < 20; i++) {
if (i === 5) {
break;
}
console.log(i);
}
<IPython.core.display.Javascript object>
Popcorn Hack 8
%%javascript
for (let i = 0; i < 20; i++) {
if (i === 10) {
continue;
}
console.log(i);
}
<IPython.core.display.Javascript object>
Homework 3-8-1
person = {'name': 'Zach', 'age': 16}
for key in person:
print(key, person[key])
for value in person.values():
print(value)
for key, value in person.items():
print(key, value)
name Zach
age 16
Zach
16
name Zach
age 16
Homework 3-8-2
for i in range(50):
if i % 15 == 0:
print("FizzBuzz")
elif i % 5 == 0:
print("Fizz")
elif i % 3 == 0:
print("Buzz")
else:
print(i)
FizzBuzz
1
2
Buzz
4
Fizz
Buzz
7
8
Buzz
Fizz
11
Buzz
13
14
FizzBuzz
16
17
Buzz
19
Fizz
Buzz
22
23
Buzz
Fizz
26
Buzz
28
29
FizzBuzz
31
32
Buzz
34
Fizz
Buzz
37
38
Buzz
Fizz
41
Buzz
43
44
FizzBuzz
46
47
Buzz
49
%%javascript
let correctUsername = "user123";
let correctPassword = "pass123";
let attempts = 3;
let loggedIn = false;
do {
let username = prompt("Enter username:");
let password = prompt("Enter password:");
if (username === correctUsername && password === correctPassword) {
console.log("Login successful!");
loggedIn = true;
break;
} else {
attempts--;
if (attempts > 0) {
console.log(`Incorrect credentials. You have ${attempts} attempts left.`);
} else {
console.log("Account locked due to too many failed attempts.");
}
}
} while (attempts > 0 && !loggedIn);
<IPython.core.display.Javascript object>
Homework 3-8-3
tasks = [
"Go touch grass",
"Do the dishes",
"Take out the trash",
"Eat dinner",
"Go to the gym",
"Sleep"
]
def display_tasks():
print("My to do list:")
for index in range(len(tasks)):
print(f"{index + 1}. {tasks[index]}")
display_tasks()
My to do list:
1. Go touch grass
2. Do the dishes
3. Take out the trash
4. Eat dinner
5. Go to the gym
6. Sleep
Homework 3-8-4
for num in range(20):
if num % 2 == 1:
continue
print(f"This number is... {num}")
This number is... 0
This number is... 2
This number is... 4
This number is... 6
This number is... 8
This number is... 10
This number is... 12
This number is... 14
This number is... 16
This number is... 18