Sometimes, there are pieces of code that have to run multiple times. It would be a pain to write the pieces of code multiple times. What we can do instead, though, is use loops to run the code over and over again.
Create a new Repl called loops_example.
Now, write the line print("You are the high scorer!")
10 TIMES.
Run the above code. What you'll see is that "You are the high scorer" prints out 10 times. It was boring writing that out 10 times though,
right?
Let's now remove all of the print statements except one, and place it inside a For Loop.
Run the above code and notice what happens. The one line of code that we put inside the loop body
executes multiple times--10 to be exact. *Play around with the numbers in parentheses and see what happens*
Below, we take a closer look at the structure of a for loop in Python:
This for loop consists of:
As seen above, we can repeat a block of code a set number of times using a for loop. We can Also
use a for loop to repeat a block of code for each element in a list
In the below example, we loop over a list of names, and print each name in the list out. Write and run the code.
Our loop variable, robot, holds a single item in the
list instead of a number in a range. The value in robot is updated
each time around the loop, so that it
holds Bing, then Bleep, and finally
Bloop. Once it reaches the end of the
list, the loop stops. **Play around with the list and see what happens.
You can loop over strings (text) the same way you loop over list. To do so, write the following:
for char in string_variable: print(char)Set string_variable to any string (remeber: quotes!) and write the above code. Run the program and see what happens
We can loop through one list in Python pretty easily, but if we want to loop through two (or more) lists at once,
you'll need an extra variable to navigate both lists.
Write the below code in your file. This program uses a variable
called index to move through both lists, printing out each
robot’s name and also what color it is.
We advance throught both lists by indexing each list and updating the index variable.
We use for loops when we know exactly how many times we want something to repeat.
When we aren't sure how many times we want code to repeat, though, we use a while
loop.
Think of a while loop as a loop with an if statement--as long as the if statement is true, the loop body repeats.
Write the below code in your file.
This program asks the user if they want to throw a water baloon.
As long as the loop condition is met (equals true) the loop body repeats. Once it turns false, the code
continues and "Goodbye!" is printed. **Play around with the loop condition
If our loop condition can never be false, we run an infinite loop. This essentially means that our loop body runs forever.
This is generally not advised in coding, as it can overload the computer you're programming.
Write a program that takes text as input and outputs the number of vowels in the text.
Extension: Have the program keep asking for text if the text has no vowels
Copyright © KX Technology Group, LLC. All Rights Reserved.