3.8-3.10 Hacks
Iteration: Part of an Algorithm that repeats itself until a certain condition is met
- Start
- the number of petals on a flower is 8
- pick off one petal, and number of petals goes down by one
- How many petals are left?
- repeat step three until number of petals is zero
- Finish
numPetals = 8
while (numPetals > 0):
numPetals -= 1
print(numPetals)
if numPetals == 0:
print("No More Petals")
# EXTRA BONUS
if numPetals % 2 == 0:
print("I am smart")
if numPetals % 2 == 1:
print("I am dumb")
Iteration Statement: loop repeats zero or more times, until disturbed (loop is stopped)
i=3
while i<=81:
print(i)
i=i+13
colors = ["red", "orange", "yellow", "green", "blue", "purple"]
for i in colors:
if i == "green":
break
print(i)
# it will stop printing after yellow, prints until green
nums = ['38', '45', '67', '83', '78']
least = min(nums)
# minimum
print(min(nums))
# print minimum
for i in nums:
# loop to check if there are any smaller numbers
if i < (min(nums)):
#if there are any numbers that are even smaller than the one before, it will print the new one, but there isn't.
print(i)
Reference sheet notes (4)
- Append adds something to the list (at the end of the list)
- Insert adds something to the list (anywhere in the list)
- Removes whatever is at the index you put
- you can assign the values of a list to another list