Big Idea 3 Section 8
Popcorn hacks and Homework for Section 8 of Big Idea 3
Big Idea 3 Section 8
Popcorn Hack #1
%%javascript
for (let i = 0; i < 22; i++) {
console.log(i);
}
<IPython.core.display.Javascript object>
Popcorn Hack #2
smiling_faces = ['😀', '😃', '😄']
for smiling_faces in smiling_faces:
print(smiling_faces)
😀
😃
😄
Homework 1
## HW 1 Part 1
person = {'person1': 'Rohan Bojja', 'person2': "Neil Chandra"}
for key in person:
print(key, person[key])
for value in person.values():
print(value)
for key, value in person.items():
print(key, value)
person1 Rohan Bojja
person2 Neil Chandra
Rohan Bojja
Neil Chandra
person1 Rohan Bojja
person2 Neil Chandra
%%js
// HW 1 Part 2
const person = {
"person1": "Rohan Bojja",
"person2": "Neil Chandra"
}
for (const key in person) {
console.log(key + ": " + person[key])
}
<IPython.core.display.Javascript object>
While Loop Popcorn Hack #1
number = 1
while number <= 30:
if number % 2 == 0:
print(number)
number += 1
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
While Loop Popcorn Hack #2
import random
flip = ""
while flip != "heads":
flip = random.choice(["heads", "tails"])
print(f"Flipped: {flip}")
print("Yes, it finally landed on heads!")
Flipped: tails
Flipped: tails
Flipped: tails
Flipped: heads
Yes, it finally landed on heads!
Homework 2
# HW 2 Task 1
x = int(0)
while x != int(51):
x = int(x) + 1
if x % int(3) == int(0) and x % int(5) == int(0):
print("FizzBuzz")
elif x % int(3) == int(0):
print("Fizz")
elif x % int(5) == int(0):
print("Buzz")
elif x % int(7) == int(0):
print("Boom")
else:
print(str(x))
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
Fizz
# HW 1 Task 2
failed_counter = int(0)
while failed_counter < int(3):
if password == "Password1!":
print("You got the correct Password")
break
else:
password = input("Please enter the password here: ")
failed_counter = int(failed_counter) + 1
print("You have got it wrong " + str(failed_counter) + " times")
print("You have " + str(int(3) - int(failed_counter)) + " times left")
You have got it wrong 1 times
You have 2 times left
You have got it wrong 2 times
You have 1 times left
You got the correct Password
Python Popcorn Hack
# List of tasks
classes = [
"AP Calculus",
"AP CSP",
"AP Chemistry",
"AP English Seminar",
"World History"
]
# Function to display tasks with indices
def display_tasks():
print("Your Classes for the Day:")
for index in range(len(classes)):
print(f"{index + 1}. {classes[index]}") # Display task with its index
# Call the function
display_tasks()
Your Classes for the Day:
1. AP Calculus
2. AP CSP
3. AP Chemistry
4. AP English Seminar
5. World History
Javascript Popcorn Hack
%%javascript
// List of tasks
const classes = [
"AP Calculus",
"AP CSP",
"AP Chemistry",
"AP English Seminar",
"World History"
];
// Function to display tasks with indices
function displayTasks() {
console.log("Your To-Do List:");
for (let index = 0; index < classes.length; index++) {
console.log(`${index + 1}. ${classes[index]}`); // Display task with its index
}
}
// Call the function
displayTasks();
<IPython.core.display.Javascript object>
Popcorn Hack #1
%%javascript
for (let i = 0; i < 20; i++)
for (let i = 0; i < 20; i++) {
if (i === 15) {
break;
}
console.log(i);
}
<IPython.core.display.Javascript object>
Popcorn Hack #2
%%javascript
for (let i = 0; i < 20; i++) {
if (i === 10) {
continue;
}
console.log(i);
}
<IPython.core.display.Javascript object>
Homework 3
%%javascript
for (let i = 0; i < 20; i += 2) {
if (i === 10) {
break;
}
console.log(i);
}
<IPython.core.display.Javascript object>
for i in range(10):
if i == 5:
continue
print(f"This number is {i}")
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