For Loops

For loops are a way to repeat a block of code. There is an iteration variable that keeps track of how many times the loop has been repeated. Specifically for a for-loop, there will be some limit to how many times the loop is repeated. That something can be a given number(repeat this block of code 50 times) or something the program is iterating through(repeat this code for each letter in this word)
For loops are a way to make your code much more efficient and to save you, the programmer, a lot of time when
you are creating a program. We have used for-loops when making our spirals(I promise, this is the last time
I'm going to talk about spirals). Below is the code for the for-loops we were using. The first line of the loop
had a couple of important things. First of all, the for
keyword is a Python keyword that notifies
the computer that a for-loop is coming. x
is the iteration variable(the counter that keeps track
of how many times the loop is being repeated). Every time a loop through the code is done, the iteration variable
increases by one, unless you specify it to increase by something else. range(50)
told the computer
to repeat the code inside the for-loop 50 times. At the very end of the statement there is a colon(:). This indicates
the end of the statement, and when you hit enter, the next line will be indented. This should happen, indentation
is how Python organizes code. Make sure everything inside a loop is indented.
Note: in Python, numeric counting works different than how we count normally. When you give a number n, Python starts counting from 0 and goes up to n-1. If you count, it ends up being 50 numbers. In the example above, when given the range up to 50, Python will start at 0 and go up to 49. If you want to start counting at 1, you can specify that when you set up the for-loop.
for x in range(50):
t.forward(x + 3)
t.left(360/4 + 5)
Here are some more examples of loops using a given numerical range:
for i in range(10):
print("hello")
for i in range(10):
print(i)
The first loop prints "hello" 10 times. The second loop prints the iteration variable, and that code repeats 10 times. So, the code would print numbers 0 through 9
Here are some examples of for-loops iterating through lists:
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)
for num in numbers:
print(num * 2)
The first line defines a list, another Python data type that stores data of all sorts. It can store data of all types, and once a list is created, you can add things to it or take things out. We will cover this more in detail in future lessons.
The first for-loop iterates through the list and prints out each element in the list. The second loop prints the result of the list element times two.
Crazy Greeting Program
For this project, your goal is to create a program that asks the user for their name, and then repeats
"hello *user*" as many times as the number of letters in their name.
Hint: len()
returns the length of a string.
- Ask the user for their name and store it in a variable
- Figure out how many letters are in the name(hint: use the length function)
- Create a for loop and use that number of letters as how many iterations the loop will make
- Print “hello [name]”
Here is an example of what it should look like:
