# Loops

Loops are an essential part of programming. Basically a loop lets you run a specific
part of your program until a certain condition is met. The first loop we will be
working with is…

while Statements

``````option = 1
while option != 0:
print "2. Find perimeter and area of a rectangle"
print "0. Forget it!"
print "*" * 28
option = input("Please make a selection: ") #Prompt user for a
selection
if option == 1: #If option is 1, get input and calculate
firstnumber = input("Enter 1st number: ")
secondnumber = input("Enter 2nd number: ")
results
elif option == 2: #If option is 2, get input and calculate
length = input("Enter length: ")
width = input("Enter width: ")
perimeter = length * 2 + width * 2
area = length * width
print "The perimeter of your rectangle is", perimeter #show
results
print "The area of your rectangle is", area
else: #if the input is anything else its not valid

print "That is not a valid option!"``````

All we had to do was wrap most of our program in a while statement. ( I also added
some “/n” to the top for aesthetic reasons). This while statement will continue to loop
until the user’s picks 0 as an option.  Now we can run the program indefinitely until
we decide to quit! Let’s add a little bit more to our code so that we can add as many
numbers as we like together, instead of just two. Assign a variable with any number
besides 0, and a variable that equals 0 at the top of our code:

``````number = 1
sum = 0``````

Now change your first if statement to this:

``````if option == 1:
while number != 0:
print "Use 0 to quit adding numbers."
number = input("Enter number: ")
sum = sum + number
print "The total is: ", sum``````

If you did this correctly, when you choose option 1, you should be able to add
unlimited numbers together. The only way to exit that part of the program is to input

1. Which will then print the total. The number 1 that we assigned at the beginning
never gets added because the user inputs a number that changes number’s value
before it gets added to sum.
Oops. While testing that last code, i noticed one thing that needs debugged! (This
happens to the best programmer. At least half the time spent making a program is
debugging!) When we enter 0 to quit it says “That is not a valid option” before the
program quits. The way around this is with the breakcommand. Add this
new elif before your else statement:
``````elif option == 0:
break``````

This will effectively break us out of the loop before it reads the else statement! Let’s
see a fresh example of the while statement:

``````counter = input("Enter a number to start the countdown: ")
while counter >= 0:
print counter
counter = counter - 1 #alternatively you can use counter += -1, same
thing
print "SPLAT!"``````

This will loop the while statements x number of times, x being the user input.

## for Statements

There is another way we could have done the last program and that is with
a for statement. Try this on for size:

``````counter = input("Enter a number to start the countdown: ")
for x in range(counter, 0, -1):
print x
print "SPLAT"``````

Now there are a couple of new things in this example first and foremost, for.
What for does is assigns x to each number in a list of numbers (temporarily), or even a
list of words(more on that later). Also notice that you do not need to assign anything
to x. for does that for you! You can use any variable you want in its place.

Now what list am i talking about? I don’t see any list? Ah, but that is what the range function is for. The first field in range is the starting number, the second field is for the number right before the ending number (it doesn’t count the last number). And the third field is for the increments you want to move. The third field is optional, if you do not put anything there it defaults to increments of 1. Lets see an
example of how that works:

``````for x in range(1, 10):
print x``````

What do you suppose the output will be? This:

``````1
2
3
4
5
6
7
8
9``````

Like I said before it won’t count the last number, so if we wanted to count to 10 we
would have to use range(1, 11). Let’s try one more and then I am going to loose you
to your practice. 😉 Lets count odd numbers up to a user specified number:

``````for anything in range(1, number, 2):
print anything``````

Ok…hopefully you have a good idea of how these loops work because here is your …

Practice

1. Design a program that gives a menu for counting down and counting up to a user
specified number (extra credit if you make count up and count down a submenu), also
have an option that gives the output of the fibonacci sequence (0, 1, 1, 2, 3, 5, 8, 13,
21–the next number in the sequence is always the sum of the two numbers before it),
and then give an option to quit of course! 😉