Chapter 4 Flashcards
What is a condition controlled loop?
- Uses a true/false condition to control the number of times that it repeats.
- Uses a while statement
- While a condition is true, do some task.
- The loop has two parts: (1) a condition that is tested for a true or false value, and (2) a statement or set of statements that is repeated as long as the condition is true.
- In order for this loop to stop executing, something has to happen inside the loop to make the expression keep_going == ‘y’ false for example
What is a count controlled loop?
repeats a specific number of times
-Uses a for statement
What is an infinite loop?
If a loop does not have a way of stopping, it is called an infinite loop. An infinite loop continues to repeat until the program is interrupted. Infinite loops usually occur when the programmer forgets to write code inside the loop that makes the test condition false. In most circumstances, you should avoid writing infinite loops.
How does the for loop work?
he first time the for loop iterates, the num variable is assigned the value 1 and then the statement in line 6 executes (displaying the value 1). The next time the loop iterates, num is assigned the value 2, and the statement in line 6 executes (displaying the value 2).
What does the range function do?
creates a type of object known as an iterable. An iterable is an object that is similar to a list. It contains a sequence of values that can be iterated over with something like a loop. -Ex: for num in range(5): print(num) -Means the same thing as: for num in [0, 1, 2, 3, 4]: print(num) -You can also be more specific about the range values: for num in range(1, 5): print(num) -If you input a third number into the range, it creates a step value for the range to increment by: for num in range(1, 10, 2): print(num) -You can also use range to go from highest to lowest value: range(10, 0, −1) Output: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
11 for number in range(1, 11): 12 square = number**2 13 print(f'{number}\t{square}')
What does the \t do?
It tabs it so it is spaced out
What is an accumulator?
Tracks the total input throughout the course of a for loop:
total = 0.0
for counter in range(MAX):
15 number = int(input(‘Enter a number: ‘))
16 total = total + number
-In this case, total is the accumulator
Provide some examples of augmented assignment operators:
Operator Example Usage Equivalent To \+= x += 5 x = x + 5 −= y −= 2 y = y − 2 *= z *= 10 z = z * 10 /= a /= b a = a / b %= c %= 3 c = c % 3 //= x //= 3 x = x // 3 **= y **= 2 y = y**2
What is a sentinel?
-a special value that marks the end of a sequence of items.
EX:
22 print(‘Enter the next lot number or enter 0 to end.’)
23 lot = int(input(‘Lot number: ‘))
-Entering zero is a sentinel and will end the loop
What is priming read?
-get the first input value that will be tested by the validation loop. If that value is invalid, the loop will perform subsequent input operations.
What is a nested loop?
A nested loop is a loop that is inside another loop.
-Ex:
for hours in range(24):
for minutes in range(60):
for seconds in range(60):
print(hours, ‘:’, minutes, ‘:’, seconds)
-An inner loop goes through all of its iterations for every single iteration of an outer loop.
Inner loops complete their iterations faster than outer loops.
To get the total number of iterations of a nested loop, multiply the number of iterations of all the loops.
for x in range(8):
turtle. forward(100) turtle. right(45)
What does this code do?
Turtle code that draws a hexagon because it is creating an 8 sided shape by drawing 8 lines repeatedly turning at a 45 degree angle