Big Idea 3 Section 10
Popcorn hacks and Homework for Section 10 of Big Idea 3
Big Idea 3 Section 10
Homework 1
data_list = [42, "apple", 3.14, True, "banana", 7]
print(data_list)
user_data = input("Please enter an index number to remove the item from the list: ")
index = int(user_data)
if 0 <= index < len(data_list):
removed_item = data_list.pop(index)
print(f"Removed item: {removed_item}")
print(data_list)
[42, 'apple', 3.14, True, 'banana', 7]
Removed item: 3.14
[42, 'apple', True, 'banana', 7]
Homework 2
answers ← [“yes”, “no”] questions ← [“Do you like Python?”, “Do you like Javascript?”]
DISPLAY (“Question 1:”, questions[0]) DISPLAY (“Answer:”, answers[0])
DISPLAY (“Question 2:”, questions[1]) DISPLAY (“Answer:”, answers[1])
answers = ["yes", "no"]
questions = ["Do you like Python?", "Is the sky blue?"]
print("Question 1:", questions[0])
print("Answer:", answers[0])
print("Question 2:", questions[1])
print("Answer:", answers[1])
Question 1: Do you like Python?
Answer: yes
Question 2: Is the sky blue?
Answer: no
Homework 3
nums ← [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] even_sum ← 0 odd_sum ← 0
FOR EACH num IN nums IF num MOD 2 = 0 THEN even_sum ← even_sum + num ELSE odd_sum ← odd_sum + num END IF END FOR
DISPLAY (“Sum of even numbers:”, even_sum) DISPLAY (“Sum of odd numbers:”, odd_sum)
nums = [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]
even_sum = 0
odd_sum = 0
for num in nums:
if num % 2 == 0:
even_sum += num
else:
odd_sum += num
print("Sum of even numbers:", even_sum)
print("Sum of odd numbers:", odd_sum)
Sum of even numbers: 240
Sum of odd numbers: 225
Homework 4
hobbies = ["Reading", "Hiking", "Cooking", "Gaming", "Photography"]
for hobby in hobbies:
print(hobby)
Reading
Hiking
Cooking
Gaming
Photography
Popcorn Hack #1
print("Adding Numbers In List Script")
print("-"*25)
numlist = []
while True:
start = input("Type the number for the result - (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 24 to list.
Added 35 to list.
Added 78 to list.
Sum of numbers in list is 137
Added 22 to list.
Removed 22 from list.
Popcorn Hack #2
nums ← [1, 2, 3, …, 100] even_sum ← 0
FOR EACH num IN nums IF num MOD 2 = 0 THEN even_sum ← even_sum + num END IF END FOR
DISPLAY (even_sum)
nums = list(range(1, 101))
even_sum = 0
for num in nums:
if num % 2 == 0:
even_sum += num
print(even_sum)
2550
Popcorn Hack #3
import random
main_dishes = []
sides = []
while True:
main = input("Enter a main dish (or type 'done' to finish): ")
if main.lower() == 'done':
break
main_dishes.append(main)
while True:
side = input("Enter a side dish (or type 'done' to finish): ")
if side.lower() == 'done':
break
sides.append(side)
meal_combinations = [f"{main} with {side}" for main in main_dishes for side in sides]
print(random.choice(meal_combinations))
spaghetti with lettuce