Lesson Four Homework
Lesson Four hacks
Hacks 3.8.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.
- Iteration logic example: packing boxes
- grab a box
- add two blankets
- add one sleep mask
- repeat steps one, two, and three until you have 10 packed boxes
- pack the boxes into the truck
- 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")
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)
i = 3
while i <= 81:
print(i)
i += 13
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)
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")