#3.8.1 popcorn hack 1
counter = 1 #making variable start counting at 1
while counter <= 5: #while loop up until it repeats 5 times
print("Hello") #print hello in terminal
counter += 1 #add one after each run
Hello
Hello
Hello
Hello
Hello
#3.8.2 popcorn hack 2
outer_counter = 0 #start counting at 0, different variable for each part of my name
name = 0
middle_name = 0
last_name = 0
while outer_counter < 4: #repeat 4 times
print("My name")
name = 0 #reset counting to 0
while name < 1: #repeat once
print(" Joanna")
name += 1
middle_name = 0 #reset to 0
while middle_name < 2: #repeat twice
print(" Youfang")
middle_name += 1
last_name = 0 #reset to 0
while last_name < 3: #repeat 3 times
print(" Hu")
last_name += 1
outer_counter += 1
My name
Joanna
Youfang
Youfang
Hu
Hu
Hu
My name
Joanna
Youfang
Youfang
Hu
Hu
Hu
My name
Joanna
Youfang
Youfang
Hu
Hu
Hu
My name
Joanna
Youfang
Youfang
Hu
Hu
Hu
#3.8.3 popcorn hack 3
name = "Joanna" #variable, name in string
for letter in name: #for loop, letter is temp var to iterate through
print(letter)
J
o
a
n
n
a
#3.8.4 popcorn hack 4
fruit = { #dictionary
"apples": 28,
"lemons": 34,
"grapes": 14,
"kiwis": 52,
}
print(fruit.items()) #print everything in dictionary
for fruit, number in fruit.items(): #iterate them with for loop
print(f"Joanna bought {number} {fruit}")
dict_items([('apples', 28), ('lemons', 34), ('grapes', 14), ('kiwis', 52)])
Joanna bought 28 apples
Joanna bought 34 lemons
Joanna bought 14 grapes
Joanna bought 52 kiwis
#3.8.5 big hack 1 while loops
#Create a python script that asks a user for a password in the terminal
# 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 = "supersecretstuff" #set variable to coreect password
# while loop that repeats until correct password is entered
while True:
user_input = input("What is the password? ") #user input
# Check if the entered password is correct
if user_input == password: #if else statement check if correct password
print("Correct! you may enter the secret chamber")
break # Exit the loop if the password is correct
else: #else (if wrong) prompt user to try again
print("Incorrect, try again!")
Incorrect, try again!
Incorrect, try again!
Correct! you may enter the secret chamber
#3.8.5 homework hack 2: for loops
#create a 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_input = input("Enter your name: ") #get user input
for letter in user_input: #for loop, letter is temp var to iterate through
print(letter)
j
o
a
n
n
a
#3.8.5 homework 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
fruits = ["banana", "apple", "kiwi", "grapes", "dragonfruit", "orange", "honeydew", "mango"]
for fruit in fruits:
print(fruit)
banana
apple
kiwi
grapes
dragonfruit
orange
honeydew
mango