views
- If you are using IDLE, make sure all subprograms are off.
- For a loop that goes for a definite amount of times, you will need a for loop, and for a loop that goes on forever you'll need a while loop.
- You can use the break command to forcibly stop a loop.
- Remember the "if", "elif" and "else" words for your loops. They are useful keywords that you can use on your on your loop.
Open your shell or program.
This may be IDLE or any program. Make sure all subprograms are off if using IDLE.
Write a for loop.
If you need to loop a definite amount of times, you need a for loop. This is the structure for a for loop. The example prints hello, world! 10 times:
Write a while loop.
If you need something to loop forever, or until a condition is met, you need a while loop. A method for both is shown. That will throw a SyntaxError unless you use Python version 2. It is recommended to use the above example of the print statement.
Make the while loop last until a condition is met.
This will loop forever, or until the program ends. In this example, True will always be True as long as the variable's answer and grade are Yes and 6. If they never changed, it will run forever so the while loop just makes the code loop while something is happening.
Stop a loop.
If you need to stop a loop, use Ctrl+C. You can always kill the loop in Task Manager as well. The other way is using the break command or using while loops.
Comments
0 comment