Test_popcorn_hacks_ipynb_2_
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}
# Looping through keys using a range
keys = list(person.keys())
for i in range(len(keys)):
print(keys[i], person[keys[i]])
# Looping through values using a range
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 };
// Looping through keys using Object.keys
const keys = Object.keys(person);
for (let i = 0; i < keys.length; i++) {
console.log(keys[i], person[keys[i]]);
}
// Looping through values using Object.values
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
# Task 1: FizzBuzz with a Twist (while loop)
# Initialize the starting number
number = 50
# Loop through numbers from 50 to 100
while number <= 100:
output = ""
# Check conditions
if number % 4 == 0: # Change multiple of 3 to 4
output += "Fizz"
if number % 5 == 0:
output += "Buzz"
if number % 7 == 0:
output += "Boom"
# If output is empty, print the number
if output == "":
print(number)
else:
print(output)
# Increment the number
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
# Task 2: User Authentication System
# Predefined credentials
correct_username = "user123"
correct_password = "pass123"
max_attempts = 3
# User login function
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
# Password reset function
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!