Homework Hacks 3.8
Homework Hacks 3.8
Hacks 3.8.1
# Popcorn Hack #1
# This is a while loop that will run 5 times
counter = 5
# This is the while loop
while counter > 0:
# This will print "Hello" 5 times
print("Hello")
# This will subtract 1 from the counter every time the loop runs
counter -= 1
Hello
Hello
Hello
Hello
Hello
Hacks 3.8.2
#Popcorn Hack #2
#Strings for the varaibles in the loops
first_name = "Pranav"
middle_name = "Ponnath"
last_name = "Santhosh"
#This is a for loop that will run 4 times
#This will print the first name once, the middle name twice, and the last name three times
for _ in range(4):
print(first_name)
for _ in range(2):
print(middle_name)
for _ in range(3):
print(last_name)
Pranav
Ponnath
Ponnath
Santhosh
Santhosh
Santhosh
Pranav
Ponnath
Ponnath
Santhosh
Santhosh
Santhosh
Pranav
Ponnath
Ponnath
Santhosh
Santhosh
Santhosh
Pranav
Ponnath
Ponnath
Santhosh
Santhosh
Santhosh
Hacks 3.8.3
name = "Pranav Santhosh" #This is the string that will be printed
for letter in name: #This is a for loop that will run for every letter in the
print(letter) #This will print every letter in the string
P
r
a
n
a
v
S
a
n
t
h
o
s
h
Hacks 3.8.4
student_info = {
"John": 15,
"Abraham": 17,
"Bill": 14,
"Henry": 16
}
for name, age in student_info.items():
print(f"{name} is {age} years old. Keep up the good work!")
John is 15 years old. Keep up the good work!
Abraham is 17 years old. Keep up the good work!
Bill is 14 years old. Keep up the good work!
Henry is 16 years old. Keep up the good work!
Hacks 3.8.5
# Hack #1
correct_password = "securepassword"
user_password = ""
while user_password != correct_password:
user_password = input("Enter your password: ")
if user_password == correct_password:
print("The password is correct.")
else:
print("The password is incorrect. Please try again.")
The password is incorrect. Please try again.
The password is incorrect. Please try again.
The password is correct.
# Hack #2
name = input("Enter your name: ")
for letter in name:
print(letter)
P
r
a
n
a
v
# Hack #3
fruits = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape"]
for fruit in fruits:
print(fruit)
apple
banana
cherry
date
elderberry
fig
grape