Chapter 4 Flashcards

1
Q

What is a condition controlled loop?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a count controlled loop?

A

repeats a specific number of times

-Uses a for statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an infinite loop?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does the for loop work?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the range function do?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
11  for number in range(1, 11):
 12      square = number**2
 13      print(f'{number}\t{square}')

What does the \t do?

A

It tabs it so it is spaced out

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is an accumulator?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Provide some examples of augmented assignment operators:

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a sentinel?

A

-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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is priming read?

A

-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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a nested loop?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

for x in range(8):

 turtle. forward(100)
 turtle. right(45)

What does this code do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly