Skip to the content.
Home Accounts Setup Verify Play Hacks

Gaheera Babbar P4 Popcorn Hacks 3.8

3.8 Lesson Popcorn and Homework Hacks

Python

#Python Popcorn Hack 1: While loop repeating "hello" 5 times
# Counter variable
count = 0

# Start the while loop
while count < 5:
    print("hello")
    count += 1  # Increment the counter



hello
hello
hello
hello
hello
%%js
//JS Popcorn Hack 1: While loop repeating "hello" 5 times
// Initialize a counter variable
let count = 0;

// Start the while loop
while (count < 5) {
    console.log("hello");
    count++;  // Increment the counter
}

#Python Popcorn Hack 2: Name Repeat
# Initialize a counter for the outer loop
outer_count = 0

# Start the outer loop to repeat the whole sequence 4 times
while outer_count < 4:
    # Print first name once
    first_name_count = 0
    while first_name_count < 1:
        print("Gaheera")
        first_name_count += 1

    # Print middle name twice
    middle_name_count = 0
    while middle_name_count < 2:
        print(" ")
        middle_name_count += 1

    # Print last name three times
    last_name_count = 0
    while last_name_count < 3:
        print("Babbar")
        last_name_count += 1

    # Increment the outer loop counter
    outer_count += 1

Gaheera
 
 
Babbar
Babbar
Babbar
Gaheera
 
 
Babbar
Babbar
Babbar
Gaheera
 
 
Babbar
Babbar
Babbar
Gaheera
 
 
Babbar
Babbar
Babbar
#Python Popcorn Hack 3: Iterates and Print out Letters of my Name
for letter in "Gaheera":
    print(letter)
%%js
// JS Popcorn Hack 3: Iterates and Print out Letters of my Name
for (let letter of "Gaheera") {
    console.log(letter);
}


<IPython.core.display.Javascript object>
#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
# Create a dictionary
student_grades = {
    "Student 1": "A",
    "Student 2": "B+",
    "Student 3": "A-",
    "Student 4": "B",
    "Student 5": "A+"
}

# Loop through each key-value pair in the dictionary
for student, grade in student_grades.items():
    # Print the key (student), value (grade), and a custom message
    print(f"{student} received a grade of {grade}. Well done!")

Student 1 received a grade of A. Well done!
Student 2 received a grade of B+. Well done!
Student 3 received a grade of A-. Well done!
Student 4 received a grade of B. Well done!
Student 5 received a grade of A+. Well done!
# Python Hack 1: Password Check
# Set the correct password
correct_password = "pythonhack"

# Initialize a variable to control the loop
password_is_correct = False

# Start the while loop
while not password_is_correct:
    # Ask the user to input the password
    user_input = input("Please enter the password: ")
    
    # Check if the password is correct
    if user_input == correct_password:
        print("The password is correct!")
        password_is_correct = True  # Break out of the loop
    else:
        print("Incorrect password, please try again.")


Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
Incorrect password, please try again.
#Hack 2: Name
# Ask the user for their name
user_name = input("Please enter your name: ")

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

#Hack 3: Fruits
# Define a list of fruits
fruits = ["Apple", "Banana", "Orange", "Mango", "Grapes", "Pineapple", "Strawberry"]

# Loop through the list and print each fruit
for fruit in fruits:
    print(fruit)