3.8 Homework
3.8 Homework
3.8.1 For Loops Homework
person = {'name': 'Akshaj', 'age': 15, 'hobby': 'Tennis', 'city': 'San diego'}
# 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 Akshaj
age 15
hobby Tennis
city San diego
Akshaj
15
Tennis
San diego
name Akshaj
age 15
hobby Tennis
city San diego
3.8.2 While,Do-While
Task 1
num = 50
while num <= 100:
output = ""
if num % 3 == 0:
output += "Bang!💣 "
if num % 4 == 0:
output += "Fizz!🍾 "
if num % 6 == 0:
output += "Buzz!🐝 "
if num % 7 == 0:
output += "Boom!💥 "
if output == "":
print(num)
else:
print(output)
num += 1
50
Bang!💣
Fizz!🍾
53
Bang!💣 Buzz!🐝
55
Fizz!🍾 Boom!💥
Bang!💣
58
59
Bang!💣 Fizz!🍾 Buzz!🐝
61
62
Bang!💣 Boom!💥
Fizz!🍾
65
Bang!💣 Buzz!🐝
67
Fizz!🍾
Bang!💣
Boom!💥
71
Bang!💣 Fizz!🍾 Buzz!🐝
73
74
Bang!💣
Fizz!🍾
Boom!💥
Bang!💣 Buzz!🐝
79
Fizz!🍾
Bang!💣
82
83
Bang!💣 Fizz!🍾 Buzz!🐝 Boom!💥
85
86
Bang!💣
Fizz!🍾
89
Bang!💣 Buzz!🐝
Boom!💥
Fizz!🍾
Bang!💣
94
95
Bang!💣 Fizz!🍾 Buzz!🐝
97
Boom!💥
Bang!💣
Fizz!🍾
Task 2
correct_username = "Akshaj"
correct_password = "Akshaj is the best"
attempts = 0
max_attempts = 3
# Do-while simulation
while True:
username = input("Enter your username: ")
password = input("Enter your password: ")
if username == correct_username and password == correct_password:
print("Login successful!")
break
else:
attempts += 1
attempts_left = max_attempts - attempts
if attempts_left > 0:
print(f"Incorrect username or password. You have {attempts_left} attempts left.")
else:
print("Your account is locked.")
break
# Challenge: Password Reset after 3 failed attempts
if attempts == max_attempts:
security_answer = input("What is your favorite color? ")
# Example security question to reset the password
if security_answer == "blue":
new_password = input("Enter your new password: ")
correct_password = new_password
print("Your password has been reset.")
else:
print("Security answer is incorrect. Account remains locked.")
Login successful!
3.8.3 Index Loops Homework
Python Hack
# List of tasks
tasks = [
"Go to School",
"Come Back from school",
"Do Homework",
"Eat Dinner",
"Brush my Teeth",
"And then sleep"
]
# Function to display tasks with indices
def display_tasks():
print("Your To-Do List:")
for index in range(len(tasks)):
print(f"{index + 1}. {tasks[index]}") # Display task with its index
# Call the function
display_tasks()
Your To-Do List:
1. Go to School
2. Come Back from school
3. Do Homework
4. Eat Dinner
5. Brush my Teeth
6. And then sleep
Javascript Hack
%%javascript
// List of tasks
const tasks = [
"Go to School",
"Come Back from school",
"Do Homework",
"Eat Dinner",
"Brush my Teeth",
"And then sleep"
];
// Function to display tasks with indices
function displayTasks() {
console.log("Your To-Do List:");
for (let index = 0; index < tasks.length; index++) {
console.log(`${index + 1}. ${tasks[index]}`); // Display task with its index
}
}
// Call the function
displayTasks();
<IPython.core.display.Javascript object>
3.8.4 Homework
Javascript Hack
%%javascript
let number = 0;
while (true) { // Infinite loop
console.log(number); // Print the current number
number += 2; // Increment by 2
if (number > 10) { // Condition to break the loop
break; // Exit the loop if number exceeds 10
}
}
<IPython.core.display.Javascript object>
Python Hack
for number in range(15):
if number % 2 != 0:
continue # Skip odd numbers
print(f"This number is {number}")
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