Lesson 3.10 Popcorn Hacks and Homework

Popcorn Hack #1

print("Adding Numbers In List Script")
print("-"*25)
numlist = []
while True:
    start = input("Would you like to (1) enter numbers (2) add numbers (3) remove last value added or (4) exit: ")
    if start == "1":
        val = input("Enter a numeric value: ") # take input while storing it in a variable
        try: 
            test = int(val) # testing to see if input is an integer (numeric)
        except:
            print("Please enter a valid number")
            continue # 'continue' keyword skips to next step of while loop (basically restarting the loop)
        numlist.append(int(val)) # append method to add values
        print("Added "+val+" to list.")
    elif start == "2":
        sum = 0
        for num in numlist: # loop through list and add all values to sum variable
            sum += num
        print("Sum of numbers in list is "+str(sum))
    elif start == "3":
        if len(numlist) > 0: # Make sure there are values in list to remove
            print("Removed "+str(numlist[len(numlist)-1])+" from list.")
            numlist.pop()
        else:
            print("No values to delete")
    elif start == "4":
        break # Break out of the while loop, or it will continue running forever
    else:
        continue
Adding Numbers In List Script
-------------------------
Added 2 to list.
Removed 2 from list.
Sum of numbers in list is 0
Added 24 to list.
Added 12 to list.
Added 10 to list.
Added 8 to list.
Added 6 to list.
Added 4 to list.
Added 2 to list.

Popcorn Hack #3

import random

# Define tops and bottoms
tops = ['tanktop', 'shortsleeve', 'longsleeve', 'sweater']
bottoms = ['jeans', 'sweatpants', 'leggings', 'shorts']

# Create outfit combinations using list comprehension
outfit_combinations = [f"{top} with {bottom}" for top in tops for bottom in bottoms]

# Print a random outfit combination
print(random.choice(outfit_combinations))
longsleeve with shorts

3.10.1 Homework (List Operations)

# Initial list with various data values
data_list = ['apple', 42, True, 3.14, 'banana', False, 100]

# Display the list to the user
print("Current list:", data_list)

# Ask the user to input an index
try:
    index = int(input("Enter an index to remove: "))

    # Check if the index is within the valid range
    if 0 <= index < len(data_list):
        # Remove the item at the specified index
        removed_item = data_list.pop(index)
        print(f"Removed item: {removed_item}")
    else:
        print("Invalid index. Please enter a number within the list's range.")
    
except ValueError:
    print("Please enter a valid number.")

# Display the updated list
print("Updated list:", data_list)
Current list: ['apple', 42, True, 3.14, 'banana', False, 100]
Removed item: 42
Updated list: ['apple', True, 3.14, 'banana', False, 100]

3.10.2 Homework (Pseudocode)

  1. Initialize an empty list called numbers
  2. For i from 1 to 30: a. Append i to the numbers list
  3. Initialize sum_even to 0
  4. Initialize sum_odd to 0
  5. For each number in numbers: a. If the number is even: i. Add the number to sum_even b. Else: i. Add the number to sum_odd
  6. Print sum_even
  7. Print sum_odd
# Initialize the list
numbers = [i for i in range(1, 31)]

# Initialize sums
sum_even = 0
sum_odd = 0

# Calculate the sums
for number in numbers:
    if number % 2 == 0:
        sum_even += number
    else:
        sum_odd += number

# Print the results
print("Sum of even numbers:", sum_even)
print("Sum of odd numbers:", sum_odd)
Sum of even numbers: 240
Sum of odd numbers: 225

3.10.3 Homework (List Functions)

# List of favorite hobbies
favorite_hobbies = ["Soccer", "Snowboarding", "Surfing", "Coding", "Flag Football"]

# Loop through the list and print each hobby
for hobby in favorite_hobbies:
    print("One of my favorite hobbies is:", hobby)

One of my favorite hobbies is: Soccer
One of my favorite hobbies is: Snowboarding
One of my favorite hobbies is: Surfing
One of my favorite hobbies is: Coding
One of my favorite hobbies is: Flag Football

3.10.4 Homework (List Input)

# Initialize an empty list to store answers
answers = []

# First question
question1 = input("Do you like coding? (yes/no): ").strip().lower()
answers.append(question1)

# Second question
question2 = input("Have you worked on projects before? (yes/no): ").strip().lower()
answers.append(question2)

# Print the answers
print("Your answers to the questions are:")
for idx, answer in enumerate(answers, start=1):
    print(f"Question {idx}: {answer.capitalize()}")
Your answers to the questions are:
Question 1: Yes
Question 2: Yes