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 5 to list.
Added 10 to list.
Sum of numbers in list is 15
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)
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)
def gather_tasks():
tasks = []
print("Enter your tasks (type 'done' to finish):")
while True:
task = input("Task: ")
if task.lower() == 'done':
break
tasks.append(task)
return tasks
# Function to display tasks
def display_tasks(tasks):
count = len(tasks)
print(f"You have {count} task(s) on your list:") # No color
for task in tasks:
print(f"- {task}") # No color
# Main code
if __name__ == "__main__":
user_tasks = gather_tasks()
display_tasks(user_tasks)
Enter your tasks (type 'done' to finish):
You have 3 task(s) on your list:
- eat
- sleep
- drink
Homework Hacks
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
number = int(input("Enter a number to be removed from the list: "))
if number in myList:
print("Removed the number from the list")
myList.remove(number)
print("This is the new list", myList)
else:
print("No number removed.")
Removed the number from the list
This is the new list [1, 2, 3, 4, 5, 6, 7, 8, 9]
Pseudocode
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] total <- 0 odd_total <- 0
FOR EACH score IN nums total ← total + score IF score MOD 3 = 0 THEN odd_total ← odd_total + score END IF END FOR
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]
total = 0
odd_total = 0
for num in nums:
total += num
if num % 3 == 0:
odd_total += num
print("Total of numbers", total)
print("Total of odd numbers", odd_total)
Total of numbers 465
Total of odd numbers 165
hobbies = ["Programming", "Reading", "Basketball", "Tennis", "Movies"]
for hobby in hobbies:
print(hobby)
Programming
Reading
Basketball
Tennis
Movies
answers = ["no", "yes"]
ans_1 = input("Is the sun green?")
ans_2 = input("Is Creed a movie series?")
# Question 1
if ans_1.strip().lower() == answers[0]:
print("Correct")
elif ans_1.strip().lower() != answers[0]:
print("Wrong")
# Question 2
if ans_2.strip().lower() == answers[1]:
print("Correct")
elif ans_2.strip().lower() != answers[1]:
print("Wrong")
Wrong
Wrong