How to Do Loops in Python
In programming you can use loops to insure a repeated cycle of events until a break point is reached. One example of this would be in a game where a player attempts to get through a level. If they die the code loops them back to the start of the level and they can try again until they reach the end, which would be the break point. While the code in the loop is different for every situation the start and the end of the code are the same.
Instructions
-
-
1
Type "While true" at the start of your code. This means that your loop will happen until an event occurs that breaks up the loop.
-
2
Type in your program code. This is going to change depending on what you want to happen in your loop, but suffice to say, everything you want to loop should be placed after the "While true" statement.
-
-
3
Type "If" after your code. The "If" statement is what will determine the break in your loop. For instance, if you write "If raw_input("Continue? (Yes/No) ") == "No": break" that means that if someone selects "No" when "Continue?" appears the loop will be ended. If you don't specify an if statement the loop will continue indefinitely.
-
1
Tips & Warnings
Test your program out to make sure that the loop works properly and that the loop ends when the break is reached.