Week 6: Simple Loops Flashcards
How to search through a specific range of index numbers?
for x in range(1,11)
print(x)
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
How to search for a specific variable in a list?
for num in nums:
if nums == 3:
print(“found 3!”)
break
print(num)
1, 2, found 3!
What is a Nested Loop?
When a loop is inside another loop.
What type of loop is this and what might the output be:
for num in nums:
for letter in ‘abc’:
print(num, letter)
Nested.
1 a
1 b
1 c
2 a
2 b
2 c
How does an infinite loop happen?
When a while condition becomes False, there is no stoppage of a code.
What structure are Loops?
Repetition
How can you stop loops?
break
Define Iteration.
Path through a lap (#)
Define Loop.
Blocks of code that repeat. Can be while, for, or until statements.
Define Termination Condition.
What condition ends the loop.
Define Block.
Code that is the loop. Contains code that repeats and the code that ends the loop.
What is the body of the code?
Code that needs to repeat.
What are the 2 types of loop codes and how do they differ?
Pre-Test and Post-Test. In Pre, the condition terminating the code is at the beginning and Python only accepts this type.
Post has the condition terminating the loop at the end and always runs at least once.
What would an example of each look like?
a. Pre-Test
b. Post-Test
a.
condition code
—-repeating code
—-termination code
b.
—-repeating code
—-termination code
condition code
Give an example of a Test condition.
Until answer_by_user == “no”
or
While user_continue != “Q”
What is a Counter/Do Loop?
Easiest to write as its not up to the user, rather we configure how many times this code needs to loop (executes a set # of times).
In Do Loops, does the counter need to be declared? And if so, how might you do this?
Yes. Will need to be incrememented (Example: counter += counter).
counter = 0
What does the syntax of a Do Loop look like?
counter = int
counter = 1
while counter <= 5:
—-print(counter)
—-counter =+ 1
end loop
Tips to debug a loop?
- If it is an infinite, check to what can make it False.
- Check if loops are missing a line that implements the counter (counter = counter + 1)
- End prompt (exit strategy) is missing
- Syntax error / capitalization errors
What does “…step 1” tell the computer to do?
To increment the counter by 1 with each pass or iteration through the loop.
Which is it–While/Until or For Loop?
a. Runs a predetermined amount of times?
b. Counting variable is automatically declared?
c. Counting variable must be incremented?
a. For Loop
b. For Loop
c. While/Until
What is a Nested Loop? Give an example.
Loop within a Loop.
for noun in nouns:
—-for adj in adjs:
——–print(noun, adj)
How many times will Hello print and why:
Counter = 0
while Counter < 3:
—-print(“Hello”)
—-counter = counter + 2
Hello
Hello
1st iteration: because 0 < 3 is True. Prints once.
2nd: because now counter = 2 which is still < 3, True. Prints again.
3rd: Counter gets changes from 2 to 4 and is now False, as it is not < 3.
How many times will this print and why:
for i in range(0,5,1):
—-print(“Hello”)
Hello
Hello
Hello
Hello
Hello
because of the 2nd/middle #