##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 10 to list.
Popcorn Hack 2
- Discussed in class that we cannot do this hack in vs code
## Popcorn Hack 3
import random
do = ['sleep', 'study', 'watch TV', 'work out']
time = ['1hr', '2hr', '3hr', '4hr']
do_combinations = [f"{main} for {side}" for main in do for side in time]
print(random.choice(do_combinations))
work out for 1hr
## Homework for 3.10.1
data_list = [42, "hello", 3.14, True, "Python", [1, 2, 3], {"key": "value"}]
print("Original list:", data_list)
user_input = input("Enter an index to remove (0 to {}): ".format(len(data_list) - 1))
try:
index = int(user_input)
if 0 <= index < len(data_list):
removed_item = data_list.pop(index)
print(f"Removed item: {removed_item}")
else:
print("Error: Index out of range.")
except ValueError:
print("Error: Please enter a valid number.")
print("Updated list:", data_list)
Original list: [42, 'hello', 3.14, True, 'Python', [1, 2, 3], {'key': 'value'}]
Removed item: Python
Updated list: [42, 'hello', 3.14, True, [1, 2, 3], {'key': 'value'}]
## Homework for 3.10.2
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]
even_sum = 0
odd_sum = 0
for number in numbers:
if number % 2 == 0:
even_sum += number
else:
odd_sum += number
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 for 3.10.3
hobbies = ["Eating", "Sleeping", "Driving", "Reading", "Baking"]
print("Using a for loop:")
for hobby in hobbies:
print(hobby)
print("\nUsing a while loop:")
i = 0
while i < len(hobbies):
print(hobbies[i])
i += 1
Using a for loop:
Eating
Sleeping
Driving
Reading
Baking
Using a while loop:
Eating
Sleeping
Driving
Reading
Baking
## Homework for 3.10.4
people = []
def add_person():
height = input("How tall are you?")
color = input("What is your favorite color?")
while True:
is_pizza = input("Do you like pizza? (yes/no): ").lower()
if is_pizza in ["yes", "no"]:
is_pizza = (is_pizza == "yes")
break
else:
print("Please enter 'yes' or 'no'.")
# Create the person object
person = {
'height': name,
'color': age,
'is_pizza': is_pizza
}
people.append(person)
display_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['height']}, {person['color']}, {student_status}")