Skip to the content.

Lessons 3.8 Hacks

3.8

Popcorn Hacks

3.8.1 While loops

Hack 1

I want you to create a while loop that will print out “hello” to the terminal 5 times

variable = 1

# start while loop
while variable <= 5:
    # print value associated with variable
    print("Hello")
    
    # Increment the counter to add one as hello is printed
    variable += 1
Hello
Hello
Hello
Hello
Hello

3.8.2 Nested While loops

Hack 2

Create a piece of python code that prints out your name one time

then after printing your first name one time, print out your middle name 2 times

and finally print out your last name three times

this entire thing should be repeated 4 times

# define name
first_name = "Maryam"
middle_name = "Kushi Gade" # i dont have a middle name guys...
last_name = "Abdul-Aziz"

counter = 0

# create while loop
while counter < 4:
    print(first_name)
    
    middle_count = 0
    while middle_count < 2:
        print(middle_name)
        middle_count += 1
    
    last_count = 0
    while last_count < 3:
        print(last_name)
        last_count += 1
    
    counter += 1
Maryam
Kushi Gade
Kushi Gade
Abdul-Aziz
Abdul-Aziz
Abdul-Aziz
Maryam
Kushi Gade
Kushi Gade
Abdul-Aziz
Abdul-Aziz
Abdul-Aziz
Maryam
Kushi Gade
Kushi Gade
Abdul-Aziz
Abdul-Aziz
Abdul-Aziz
Maryam
Kushi Gade
Kushi Gade
Abdul-Aziz
Abdul-Aziz
Abdul-Aziz

3.8.3 For Loop

Hack 3

I want you to create a for loop that iterates and prints out to the terminal the letters of your name

full_name = "Maryam Abdul-Aziz"

for letter in full_name:
    print(letter)
M
a
r
y
a
m
 
A
b
d
u
l
-
A
z
i
z

3.8.4 For Loops with Lists and Dictionaries

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

%%html
<script>
student_info = {
    'name': "Maryam",
    'age': 17,
    'grade': 12,
    'hair_color_blonde': false
};

console.log(student_info);

for (const [key, value] of Object.entries(student_info)) {
    console.log(`My ${key} is ${value}.`);
}

</script>

Hacks

3.8.5 Nested For Loops

Hack 1 While Loops

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.

password = "ILoveCSP123"

inputted_password = input("Password (type 'esc' to exit):")

while inputted_password != password:
    if inputted_password == "esc":
        print("Exiting program.")
        break
    print("Incorrect password, try again.")
    inputted_password = input("Password (type 'esc' to exit): ")

if inputted_password == password:
    print("Password is correct. Welcome back!")

Password is correct. Welcome back!

Hack #2 For loops

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

user_name = input("What is your name?")

for letter in user_name:
    print(letter)
s
t
e
v
e

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

fruit_list = ["strawberry", "apple", "banana", "kiwi", "watermelon", "cherry", "orange"]

for fruit in fruit_list:
    print(fruit)
strawberry
apple
banana
kiwi
watermelon
cherry
orange