Hacks 3.8.1

  1. Iteration is a term used in computer science to simplify code by repetition. The code will use loops to repeat until a condition is met.
  2. Iteration logic example: packing boxes
    1. grab a box
    2. add two blankets
    3. add one sleep mask
    4. repeat steps one, two, and three until you have 10 packed boxes
    5. pack the boxes into the truck
  3. below:
donuts = int(input("How many donuts do you have?"))

while donuts > 0:
    print("A student recieves a donut")
    donuts -= 1

print ("You have no donuts left")
A student recieves a donut
A student recieves a donut
A student recieves a donut
A student recieves a donut
A student recieves a donut
You have no donuts left

Hacks 3.8.2

  1. An iteration statement is a statement that executes a segment of code zero or more times in order until a condition is met or there is a break in the code.
decending_list = [int(input("What number would you like to countdown from?"))]  

for i in decending_list:
    i -= 1
    decending_list.append(i)
    if i == 0:
        break
print(decending_list)
[5, 4, 3, 2, 1, 0]
i = 3

while i <= 81:
    print(i)
    i += 13 
3
16
29
42
55
68
81

Hacks Unit 3 Section 10

Quiz:

linked in review ticket

Minimum list:

  • Use the list made bellow
  • Make a variable to hold the minimum and set it to potential minimum value
  • Loop
  • Check each element to see if it is less than the minimum variable
  • If the element is less than the minimum variable, update the minimum
  • After all the elements of the list have been checked, display the minimum value
nums = ["10", "15", "20", "25", "30", "35"]

minimum = 50

for i in nums:
    if int(i) < minimum:
        minimum = int(i)

print (minimum)
10

Extra Work

I decided to go further to understand all of the list functions. Below I have coded an algorithim that incorporates sorted, append, length, and max to track monthly earnings from work.

monthly_earnings = []

a = 0
while a == 0: # this will continue asking for each month's earnings to add to the list until the user enters nothing (clicks enter)
    money = input("How much money did you make this month?")
    if money == "":
        break 
    monthly_earnings.append(int(money)) # this adds all of the input into the list
   
    

sort = input("Do you want to sort your earnings?") 

if sort == "yes": # if they want to sort earnings
    print("sorted:")
    print(sorted(monthly_earnings))
else: # if they don't want to sort earnings, it will print unsorted
    print("unsorted:")
    print(monthly_earnings)

print("Your highest monthly earning is $" + str(max(monthly_earnings)))
print("You have been working for " + str(len(monthly_earnings)) + " months")
sorted:
[1, 2, 10, 500, 10000]
Your highest monthly earning is $10000
You have been working for 5 months