Team Teach 3.10 Homework and Popcorn Hacks
Showing how I was able to work and experiment with these.
Team Teach 3.10 Popcorn hacks
3.10.1 popcorn hacks
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 1 to list.
Homework for 3.10.1
# Starting list with various data types
mylist = ["apple", 42, 3.14, True, "banana"]
# Display the list to the user
print("Current list:", mylist)
# Ask user for input
index = int(input("Enter an index number to remove an item: "))
# Check if the index is valid
if 0 <= index < len(mylist):
removed_item = mylist.pop(index) # Remove the item at the given index
print(f"Item '{removed_item}' at index {index} has been removed.")
else:
print("Invalid index. Please enter a valid number between 0 and", len(mylist) - 1)
# Display the updated list
print("Updated list:", mylist)
Current list: ['apple', 42, 3.14, True, 'banana']
Item '42' at index 1 has been removed.
Updated list: ['apple', 3.14, True, 'banana']
3.10.2 Popcorn hack
def find_max_min(numbers):
if not numbers:
return None, None # <span style="color: #D9B68C;">Return None if the list is empty</span>
max_num = numbers[0] # <span style="color: #D9B68C;">Start with the first number as the max</span>
min_num = numbers[0] # <span style="color: #D9B68C;">Start with the first number as the min</span>
for num in numbers:
if num > max_num:
max_num = num # <span style="color: #D9B68C;">Update max if a larger number is found</span>
if num < min_num:
min_num = num # <span style="color: #D9B68C;">Update min if a smaller number is found</span>
return max_num, min_num # <span style="color: #D9B68C;">Return the found max and min</span>
# Example usage
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
max_num, min_num = find_max_min(numbers)
print(f"\033[92mMaximum number: {max_num}\033[0m") # Bright green for output
print(f"\033[92mMinimum number: {min_num}\033[0m") # Bright green for output
[92mMaximum number: 9[0m
[92mMinimum number: 1[0m
Homework 3.10.2
%%js
1. Initialize a list of 30 numbers
2. Initialize a variable sum_even to 0
3. Initialize a variable sum_odd to 0
4. Loop through each number in the list
a. If the number is even (number % 2 == 0)
i. Add the number to sum_even
b. Else (the number is odd)
i. Add the number to sum_odd
5. Print sum_even
6. Print sum_odd
# Step 1: Initialize a list of 30 numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
# Step 2 and 3: Initialize sum_even and sum_odd to 0
sum_even = 0
sum_odd = 0
# Step 4: Loop through each number in the list
for number in numbers:
if number % 2 == 0: # Step 4a: Check if the number is even
sum_even += number # Add the number to sum_even
else: # Step 4b: If the number is odd
sum_odd += number # Add the number to sum_odd
# Step 5 and 6: Print the sums
print("Sum of even numbers:", sum_even)
print("Sum of odd numbers:", sum_odd)
Sum of even numbers: 240
Sum of odd numbers: 225
import random
# Define main dishes and sides
main_dishes = ['Spaghetti', 'Tacos', 'Grilled Chicken', 'Stir Fry']
sides = ['Salad', 'Rice', 'Garlic Bread', 'Steamed Vegetables']
# Create meal combinations using list comprehension
meal_combinations = [f"{main} with {side}" for main in main_dishes for side in sides]
# Print a random meal combination
print(random.choice(meal_combinations))
arshia with Arhaan
3.10.4
# Define a list to hold user information
people = []
# Function to collect user input and create a person object
def add_person():
name = input("What is your name? ")
age = input("How old are you? ")
gender = input("what's you gender?")
shoesize = input("what's your shoe size?")
# Simple Yes/No question for student status
while True:
is_student = input("Are you a student? (yes/no): ").lower()
if is_student in ["yes", "no"]:
is_student = (is_student == "yes") # Converts to Boolean
break
else:
print("Please enter 'yes' or 'no'.")
# Create the person object
person = {
'name': name,
'age': age,
'is_student': is_student
}
# Add the person to the list
people.append(person)
# Display the added person
display_people()
# Function to display the list of people
def display_people():
print("\nCurrent List of People:")
for index, person in enumerate(people, 1):
student_status = "a student" if person['is_student'] else "not a student"
print(f"Person {index}: {person['name']}, {person['age']} years old, {student_status}")
# Example Usage
add_person() # Call this to start collecting input from the user
Current List of People:
Person 1: Alex, 15 years old, a student