3.10 Popcorn and Homework Hacks
Yash's Submission for 3.10 Homework
Popcorn Hack 1
numbers = []
while True:
user_input = input("Enter a [number] to add to the list, [sum], [pop], or [quit]: ")
if user_input.lower() == 'quit':
break
elif user_input.lower() == 'sum':
print(f"The sum of the numbers is: {sum(numbers)}")
elif user_input.lower() == 'pop':
if numbers:
removed_value = numbers.pop()
print(f"Removed {removed_value} from the list.")
else:
print("The list is empty.")
else:
try:
number = float(user_input)
numbers.append(number)
print(f"Added {number} to the list.")
except ValueError: #BONUS, I ADDED THIS SO IF THE DATA ENTERED IS NOT A NUMBER, IT WILL PRINT AN ERROR MESSAGE
print("Invalid input. Please enter a number, 'sum', 'pop', or 'quit'.")
Added 55.0 to the list.
Added 77.0 to the list.
The sum of the numbers is: 132.0
Removed 77.0 from the list.
Added 22.0 to the list.
Added 33.0 to the list.
Added 44.0 to the list.
The sum of the numbers is: 154.0
Removed 44.0 from the list.
The sum of the numbers is: 110.0
Invalid input. Please enter a number, 'sum', 'pop', or 'quit'.
Popcorn Hack #2
PSEUDOCODE:
INITIALIZE an empty list called ‘numbers’ FOR each number from 1 to 100: ADD that number to ‘numbers INITIALIZE a variable called ‘even_sum’, starting value is 0 FOR each number in ‘numbers’: IF the number is even: ADD the number to even_sum PRINT the value of even sum
numbers = []
for i in range(1, 101):
numbers.append(i)
even_sum = 0
for number in numbers:
if number % 2 == 0:
even_sum += number
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 (combining random meals and user input)
import random
def gather_meals():
main_dishes = []
sides = []
print("Enter your main meal ideas (type 'done' to finish):")
while True:
main = input("Main dish:")
if main == 'done':
break
main_dishes.append(main)
print("Enter your side meal ideas (type 'done' to finish):")
while True:
side = input("Side dish:")
if side == 'done':
break
sides.append(side)
return main_dishes, sides
def suggest_random_meal(main_dishes, sides):
if not main_dishes or not sides:
print("Please enter at least one main dish and one side.")
return
meal_combinations = [f"{main} with {side}" for main in main_dishes for side in sides]
print("\nHere's a random meal suggestion for you:")
print(random.choice(meal_combinations))
if __name__ == "__main__":
mains, sides = gather_meals()
suggest_random_meal(mains, sides)
Enter your main meal ideas (type 'done' to finish):
Enter your side meal ideas (type 'done' to finish):
Here's a random meal suggestion for you:
paneer with onion rings
List Operations
Homework Hack
data_list = [10, 'apple', 3.14, 'banana', 42, 'orange', True]
print("Initial list:", data_list)
index = int(input("Enter an index number to remove the corresponding item from the list: "))
#BONUS: I ADDED A BOOLEAN TO CHECK IF THE INDEX ENTERED IS VALID
if 0 <= index < len(data_list):
removed_item = data_list.pop(index)
print(f"Removed item: {removed_item}")
else:
print("Invalid index. Please enter a number between 0 and", len(data_list)-1)
print("Updated list:", data_list)
Initial list: [10, 'apple', 3.14, 'banana', 42, 'orange', True]
Removed item: banana
Updated list: [10, 'apple', 3.14, 42, 'orange', True]
Pseudocode
Homework Hack 1 (for both Pseudocode and List Input)
PSEUDOCODE: INITIALIZE a list called ‘questions’, empty APPEND question 1 to ‘question’ APPEND question 2 to ‘question’ FOR each question in ‘questions’: PRINT question INPUT from user is answer If answer is yes: print: yes to question (question number) ELSE print: no to question (question number)
questions = []
questions.append("Do you like soccer? (yes/no)")
questions.append("Do you have a pet? (yes/no)")
for question in questions:
answer = input(question)
if answer == 'yes':
print(f"You answered yes to the question: {question}")
else:
print(f"You answered no to the question: {question}")
You answered yes to the question: Do you like soccer? (yes/no)
You answered no to the question: Do you have a pet? (yes/no)
Homework Hack 2
PSEUDOCODE: INITIALIZE empty list called numbers FOR each number from 1 to 30 ADD number to ‘numbers’
INITIALIZE ‘evensum’ and ‘oddsum’ IF number is even ADD number to ‘evensum’ ELSE ADD number to ‘oddsum’
PRINT ‘evensum’ PRINT ‘oddsum’
numbers = list(range(1, 31))
even_sum = 0
odd_sum = 0
for number in numbers:
if number % 2 == 0:
even_sum += number
else:
odd_sum += number
print(f"The sum of all even numbers is: {even_sum}")
print(f"The sum of all odd numbers is: {odd_sum}")
The sum of all even numbers is: 240
The sum of all odd numbers is: 225
List Functions
Homework Hack
# List of favorite hobbies
hobbies = ["Snowboarding", "Playing Soccer", "Music", "Gaming", "Photography"]
# Loop through the hobbies list and print each hobby
for hobby in hobbies:
print(f"One of my favorite hobbies is: {hobby}")
One of my favorite hobbies is: Snowboarding
One of my favorite hobbies is: Playing Soccer
One of my favorite hobbies is: Music
One of my favorite hobbies is: Gaming
One of my favorite hobbies is: Photography