Unit 7 Flashcards
In this unit, you will learn how to use loops to efficiently repeat code in your programs. You will also learn how to avoid infinite loops and how to exit a loop using the break command. By the end of this unit, you will be able to Use loops to perform repetitive tasks. Select the correct looping structure to accomplish a specific task. Combine multiple loops and utilise early exit conditions.
What is a loop?
A loop is a way to perform a repeated action in your program. It allows us to simplify our code.
What are the two different types of loops?
For-loops and while loops.
print(“I love Python!”)
print(“I love Python!”)
print(“I love Python!”)
What is bad about writing this code?
It is frustrating to have to write the same code three times in order to get the desired result.
It also increases your chance of making an error when you’re writing your program and it makes it more difficult to follow the logic that you’re using when you’re writing your program.
for count in range(0,3):
What are the words “for”, “count”, “in range” “(0,3)” for?
for is the command that we’re using.
“count” is the variable we’re using to keep track of how many times the loop will run.
“in range” tells Python that you want to run your loop in between a range of numbers which in this case is between 0 and 3.
The loop adds one to count each time it runs and the 0 to 3 means that count will start at 0. The loop won’t run anymore when count is equal to 3. It stops immediately.
Always remember to include the colon or your loop won’t work.
What should you code to get this as the output?
I love Python! 0
I love Python! 1
I love Python! 2
for count in range(0,3):
print(“I love Python!” + str(count))
We can use an integer _____ to replace the numbers in the ___ section of the for loop.
We can use an integer variable to replace the numbers in the range section of the for loop.
For example:
name = input(“Name:”)
times = int(input(“Number of Times: “))
for count in range (0, times):
print(name)
How do you count by twos or by threes?
The range can take a third parameter called “step”. This is how much to add to the counting variable each time the loop runs.
How do you get this as the output using the for loop?
0
2
4
6
8
for count in range(0,12,2):
print(count)
This is how much to add to the counting variable each time the loops runs.
Basically for the second number in the parameter, the programme would not print it out. So it will generate a sequence of numbers starting from 0 up to (but not including) 12.
This means it starts at 0 and adds 2 each time until it reaches or exceeds 10.
Is it true that you can loop through words as well as numbers?
yes just that the structure is a little different.
What will the output of these lines of code be?
word = input(“Enter a word:”)
for letter in word:
print (letter)
It will print out each character of a string on a separate line.
Let’s analyse these lines of code.
The first line of this code asks the user for a word.
The next part is our for statement that prints each letter separately. It tells us that for each character (which we’re temporarily storing in a variable called letter) in a string (which we stored in the variable word), do something.
In this case, the “do something” is print out the contents of the temporary variable letter.
the output will be
P
y
t
h
o
n
Explain what this code does.
word = input(“Enter a word:”)
letter_to_find = input(“Enter a letter to find: “)
for letter in word:
if letter == letter_to_find:
print(“Found it!”)
The first two lines are getting the information from the user: the word to search and what letter to search for.
instead of printing out the letter, we added an if statement to compare the letter to the letter we’re trying to find. If they are the same (if we found the letter), it prints out the statement that “Found it!”
What do you think will happen if the letter we are trying to find appears more than once? It will print out the word Found it for each letter.
For example:
Enter a word: science
Enter a letter to find: c
Output:
Found it!
Found it!
What would the output be for this?
for i in range(0,4):
print(i)
0
1
2
3
What would the output be for this?
for i in range(1,5):
print(i)
1
2
3
4
What would the output be for this?
for i in range(1,5):
print(i+1)
2
3
4
5
When can the while loop be used?
It can be used until another piece of code tells it to stop.
Typically, you want to use a _____ when there is a given start and a given end to your loop.
For loop
_____ are best used when it will run until something happens inside the loop to tell it not to run anymore.
What do we use to help control that? (Tell it to run until something happens inside the loop to tell it not to run anymore)
While loops.
We use a Boolean variable to help control that. Boolean variables can either be “True” or “False”
To generate random numbers, we first need to import Python’s random module. What is the code for that?
Then we call _________________ to produce random numbers for our needs
import random
Then we call different functions in that module to produce random numbers for our needs