Skip to the content.

3.10_ipynb_2_

popcorn hacks

# Popcorn hack 1
# Step 1: Initialize an empty list to store numbers
numbers = []

# Step 2: Loop to take user input and append to list
while True:
    user_input = input("Enter a number to add to the list (or type 'done' to finish, 'pop' to remove last entry): ")
    
    if user_input.lower() == 'done':
        break  # Exit the loop when user types 'done'
    elif user_input.lower() == 'pop':
        if numbers:
            removed_value = numbers.pop()  # Removes the last number in the list
            print(f"Removed last value: {removed_value}")
        else:
            print("The list is already empty, nothing to pop.")
    else:
        try:
            number = float(user_input)  # Convert the input to a float (can handle both integers and decimals)
            numbers.append(number)  # Append the number to the list
            print(f"Added {number} to the list.")
        except ValueError:
            print("Please enter a valid number.")

# Step 3: Calculate the sum of all numbers in the list
if numbers:
    total_sum = sum(numbers)
    print(f"Your list of numbers: {numbers}")
    print(f"The sum of all numbers in the list is: {total_sum}")
else:
    print("The list is empty.")
Added 2.0 to the list.
Added 2.0 to the list.
Added 2.0 to the list.
Added 2.0 to the list.
Added 2.0 to the list.
Added 2.0 to the list.
Added 2.0 to the list.
Added 2.0 to the list.
Added 2.0 to the list.
Added 2.0 to the list.
Added 2.0 to the list.
Added 2.0 to the list.
Added 2.0 to the list.
Added 2.0 to the list.
Added 2.0 to the list.
Your list of numbers: [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0]
The sum of all numbers in the list is: 30.0
# Popcorn Hack 2



#Initialize empty list called numbers
#For each number from 1 to 100:
#Append the number to the list 'numbers'

#Initialize even_sum as 0
#For each number in the list 'numbers':
#If the number is divisible by 2:
#Add the number to even_sum

#Print even_sum

# Step 1: Initialize an empty list and fill it with numbers from 1 to 100
numbers = list(range(1, 101))  # This will create a list with numbers 1 through 100

# Step 2: Initialize a variable to hold the sum of even numbers
even_sum = 0

# Step 3: Loop through the list and add the even numbers to even_sum
for num in numbers:
    if num % 2 == 0:  # Check if the number is even
        even_sum += num  # Add it to the sum

# Step 4: Print the sum of even numbers
print(f"The sum of all even numbers from 1 to 100 is: {even_sum}")


The sum of all even numbers from 1 to 100 is: 2550
# Popcorn hack 3


# Initialize an empty list to store meal suggestions
meal_planner = []

# Start a loop to collect meal suggestions from the user
while True:
    meal = input("Enter a meal suggestion (or type 'done' to finish): ")
    
    if meal.lower() == 'done':  # User can type 'done' to end the input
        break  # Exit the loop when 'done' is entered
    
    meal_planner.append(meal)  # Add the meal suggestion to the list

# Display the meal suggestions
if meal_planner:
    print("\nYour meal planner for the week:")
    for i, meal in enumerate(meal_planner, start=1):
        print(f"Meal {i}: {meal}")
else:
    print("No meals were added to the planner.")

Homework Hack

#Homework Hack 1


# Step 1: Initialize a list with various data values
data_list = ["apple", 42, 3.14, "banana", True, None, "cherry"]

# Step 2: Display the current list to the user
print("Current list of items:")
for index, item in enumerate(data_list):
    print(f"Index {index}: {item}")

# Step 3: Prompt the user to enter an index
user_input = input("Enter an index to remove the item from the list (or type 'exit' to quit): ")

# Step 4: Check if the user wants to exit
if user_input.lower() == 'exit':
    print("Exiting the program.")
else:
    try:
        index_to_remove = int(user_input)  # Convert input to an integer
        
        # Step 5: Check if the index is valid
        if 0 <= index_to_remove < len(data_list):
            removed_item = data_list.pop(index_to_remove)  # Remove the item at the specified index
            print(f"Removed item: {removed_item}")
        else:
            print("Error: Invalid index. Please enter a number between 0 and", len(data_list) - 1)

    except ValueError:
        print("Error: Please enter a valid integer index.")
    
# Step 6: Display the updated list
print("Updated list of items:")
for index, item in enumerate(data_list):
    print(f"Index {index}: {item}")
Current list of items:
Index 0: apple
Index 1: 42
Index 2: 3.14
Index 3: banana
Index 4: True
Index 5: None
Index 6: cherry
Removed item: 42
Updated list of items:
Index 0: apple
Index 1: 3.14
Index 2: banana
Index 3: True
Index 4: None
Index 5: cherry
#Homework hack 2




#Initialize list called numbers with 30 numbers (e.g., [1, 2, ..., 30])
#Initialize even_sum as 0
#Initialize odd_sum as 0

#For each number in numbers:
#If number is divisible by 2:
#Add number to even_sum
#Else:
#Add number to odd_sum

#Print "Sum of even numbers:", even_sum
#Print "Sum of odd numbers:", odd_sum

# Step 1: Initialize a list with 30 numbers (1 to 30)
numbers = list(range(1, 31))  # This creates a list of numbers from 1 to 30

# Step 2: Initialize sums for even and odd numbers
even_sum = 0
odd_sum = 0

# Step 3: Loop through the list and calculate sums
for number in numbers:
    if number % 2 == 0:  # Check if the number is even
        even_sum += number  # Add to even_sum
    else:  # The number is odd
        odd_sum += number  # Add to odd_sum

# Step 4: Display the results
print("Sum of even numbers:", even_sum)
print("Sum of odd numbers:", odd_sum)

#homework hack 3

# Step 1: Create a list of favorite hobbies
favorite_hobbies = ["Reading", "Cooking", "Hiking", "Gardening", "Photography"]

# Step 2: Use a loop to iterate through each hobby in the list
print("My Favorite Hobbies:")
for hobby in favorite_hobbies:
    print(hobby)  # Print each hobby

#homework hack 4

# Step 1: Create a list of questions
questions = [
    "Do you enjoy reading books? (yes/no)",
    "Are you interested in outdoor activities? (yes/no)"
]

# Step 2: Initialize an empty list to store answers
answers = []

# Step 3: Loop through the questions and collect answers
for question in questions:
    answer = input(question + " ")  # Prompt the user for an answer
    while answer.lower() not in ['yes', 'no']:  # Validate input
        print("Please answer with 'yes' or 'no'.")
        answer = input(question + " ")  # Re-prompt for a valid answer
    answers.append(answer)  # Add the valid answer to the list

# Step 4: Display the collected answers
print("\nYour answers:")
for i, answer in enumerate(answers):
    print(f"Question {i + 1}: {answer}")