3.10 Lists Popcorn Hacks

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
-------------------------

Popcorn Hack #2

Pseudocode

nums ← 1 to 100 odd_sum ← 0

FOR EACH score IN nums IF score MOD 2 ≠ 0 THEN odd_sum ← odd_sum + score END IF END FOR

DISPLAY (“Sum of odd numbers in the list:”, odd_sum)

now in python

nums = range(1, 101) # This creates a range of numbers from 1 to 100 odd_sum = 0

for score in nums: if score % 2 != 0: odd_sum += score

print(“Sum of odd numbers in the list:”, odd_sum)

PSUEDOCODE DOESN”T WORK IN VSCODE

Popcorn Hack #3

import random

# Define classes and teachers
classes = ['HPOE', 'AP CSP', 'AP Calc', 'AP Sem', 'APES']
teachers = ['Mr. Campillo', 'Mr. Mortenson', 'Ms. Nydam', 'Dr. Hall', 'Mr. Hendricks']

# Randomly select a class and a teacher
random_class = random.choice(classes)
random_teacher = random.choice(teachers)

# Print the random class-teacher combination
print(f"{random_class} with {random_teacher}")
AP Sem with Mr. Campillo