3.8 homeworks
%%javascript
const taks = [
"Eat"
"sleep"
"fun"
"efootball"
];
function display Tasks() {
console.log("Your To-do lists")
for (let index = 0; index < tasks.length; index++) {
console.log(`${index + 1}. ${tasks[index]}`); // Display task with its index
}
}
displayTasks();
<IPython.core.display.Javascript object>
i = 50
while i <= 100:
output = ""
if i % 4 == 0:
output += "Fizz"
if i % 5 == 0:
output += "Buzz"
if i % 7 == 0:
output += "Boom"
if output == "":
print(i)
else:
print(output)
i += 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 = "admin"
correct_password = "password123"
attempts = 3
while attempts > 0:
username = input("Enter username: ")
password = input("Enter password: ")
if username == correct_username and password == correct_password:
print("Login successful!")
break
else:
attempts -= 1
if attempts > 0:
print(f"Incorrect username or password. You have {attempts} attempts left.")
else:
print("Account locked. You have used all your attempts.")
Incorrect username or password. You have 2 attempts left.
Incorrect username or password. You have 1 attempts left.
Login successful!
let i = 0;
while (true) {
console.log(i);
i += 2;
if (i > 10) {
break;
}
}
Cell In[5], line 1
let i = 0;
^
SyntaxError: invalid syntax
for i in range(10):
if i % 2 == 0:
continue
print(f"This number is... {i}")
This number is... 1
This number is... 3
This number is... 5
This number is... 7
This number is... 9
person = {'name': 'Daksha', 'age': 16}
# Looping through keys
for key in person:
print(key, person[key])
# Looping through values
for value in person.values():
print(value)
# Looping through keys and values
for key, value in person.items():
print(key, value)
name Daksha
age 16
Daksha
16
name Daksha
age 16
Popcorn Hacks
%%javascript
for (let i = 5; i < 10; i++) {
console.log(i);
}
<IPython.core.display.Javascript object>
fruits = ['🍎', '🍌', '🍒']
for fruit in fruits:
print(fruit)
🍎
🍌
🍒
number = 5
while number <= 10:
if number % 2 == 0:
print(number)
number += 1
6
8
10
import random
flip = ""
while flip != "tails":
flip = random.choice(["heads", "tails"])
print(f"Flipped: {flip}")
print("Landed on tails!")
Flipped: heads
Flipped: tails
Landed on tails!
tasks = [
"eat"
"sleep"
"do sum hw"
]
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. eatsleepdo sum hw
%%javascript
const tasks = [
"Finish homework",
"Clean the room",
"Go grocery shopping",
"Read a book",
"Exercise"
];
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>
function custombreak() {
const items = ['apple', 'banana', 'grape', 'orange'];
for (let item of items) {
console.log(item);
if (item === 'grape') return;
}
}
custombreak();
Cell In[12], line 1
function custombreak() {
^
SyntaxError: invalid syntax
items = ['apple', 'banana', 'grape', 'orange']
for item in items:
if item == 'grape':
continue
print(item)
apple
banana
orange
###