Skip to the content.

Homeworks 3.8

## 3.8.1 Popcorn hack 1 Python 

# A while loop that prints "hello" 5 times using a different approach

num_times = 5  # Define how many times to print "hello"
while num_times > 0:
    print("hello")
    num_times -= 1  # Decrease the count until it reaches zero
hello
hello
hello
hello
hello
## 3.8.2 Popcorn Hack 2 Python

# Define the names
first_name = "Kushi"
middle_name = "Bobtastic"
last_name = "Gade"

# Initialize a counter
count = 0

# Repeat the process 4 times using a while loop
while count < 4:
    print(first_name)           # Print first name once
    print(middle_name * 2)     # Print middle name twice
    print(last_name * 3)       # Print last name three times
    count += 1  # Increment the counter
Kushi
BobtasticBobtastic
GadeGadeGade
Kushi
BobtasticBobtastic
GadeGadeGade
Kushi
BobtasticBobtastic
GadeGadeGade
Kushi
BobtasticBobtastic
GadeGadeGade
## 3.8.3 Python Popcorn Hack 3 
#I want you to create a for loop that iterates and prints out to the terminal the letters of your name

name = "Kushi Bobtastic Gade"

for letter in name:
    print(letter)
K
u
s
h
i
 
B
o
b
t
a
s
t
i
c
 
G
a
d
e
## 3.8.4 Popcorn Hack 4
# Create a dictionary and create a for loop that runs through each key and value of the dictionary For each iteration, print the key and value as well as a message of your choice

my_dict = {
    "name": "Kushi",
    "hobby": "Sleeping",
    "favorite_food": "Pasta",
    "city": "New York"
}

# For loop to iterate through the dictionary
for key, value in my_dict.items():
    print(f"The key is '{key}' and the value is '{value}'. Isn't that interesting?")

The key is 'name' and the value is 'Kushi'. Isn't that interesting?
The key is 'hobby' and the value is 'Sleeping'. Isn't that interesting?
The key is 'favorite_food' and the value is 'Pasta'. Isn't that interesting?
The key is 'city' and the value is 'New York'. Isn't that interesting?
## 3.8.5 Hack 1
#Create a python script that asks a user for a password in the terminal, then if the password is correct, it prints out that the password is correct. If the password is incorrect it asks for the password again.

# Define the correct password
correct_password = "kushibobtastic"

# Ask the user for the password
user_password = input("Please enter your password: ")

# Keep asking for the password until it's correct
while user_password != correct_password:
    print("Incorrect password, please try again.")
    user_password = input("Please enter your password: ")

# Once the correct password is entered
print("Password is correct!")

Password is correct!
## 3.8.5 Hack 2
#Create python script that asks for a user’s name Then iterate through the name and print it out letter by letter through the terminal

# Ask the user for their name
user_name = input("Kushi Gade: ")

# Iterate through the name and print each letter
for letter in user_name:
    print(letter)



K
u
s
h
i
 
G
a
d
e
## 3.8.5 Hack 3 
# For loops with lists. Create a python script that has a list of at least 7 different fruits then create a for loop that iterates through that list and prints out each fruit seperate to the terminal

# List of fruits
fruits = ["Apple", "Banana", "Mango", "Orange", "Grapes", "Pineapple", "Strawberry"]

# For loop to iterate through the list of fruits and print each one
for fruit in fruits:
    print(fruit)
Apple
Banana
Mango
Orange
Grapes
Pineapple
Strawberry