# Python, need help with loops

1. Oct 22, 2011

### mr.me

Hi everyone, I'm new to programming, studying python in school.

here's my first go at a loop to count up to 100 by 2, works fine

stop_count=100
count=2
while(count<=stop_count):
print count
count=count+2

But one of my practice problems is to use 'while' to
print 2 and all powers of 2 through and including 4096

I suppose I don't quite understand the math for a sequence of exponents or the correct syntax for the code, here's my try:

stop_pow=4096
base=2
while (base<=stop_pow):
print base
base= base**base+1

Which of course just printed the value of 2^3

Would someone please show me the correct code so I can understand my mistake and this type of operation.

Last edited: Oct 22, 2011
2. Oct 23, 2011

### Staff: Mentor

Code (Text):

[/noparse] tags when posting your code. This is especially important in Python, where

Code (Text):
stop_pow=4096
base=2
while (base<=stop_pow):
print base
base= base**base+1
is syntactically incorrect, while

Code (Text):
stop_pow=4096
base=2
while (base<=stop_pow):
print base
base= base**base+1
and

Code (Text):
stop_pow=4096
base=2
while (base<=stop_pow):
print base
base= base**base+1
are syntactically correct, but different.

Try to add "print base**base+1" to your loop to see what you are calculating. It is not what you think it is. And I don't see how it could print just 8, it has to start printing 2 and 5.

3. Oct 23, 2011

### mr.me

Thank-you
Well yes, sorry, it does print 2 and 5 and 8 and if I include
print base**base+1
it only prints five, indefinitely.

I see that I am raising a number (2) to itself and adding one, I am completely unaware of how to use the language to respectively raise 2 to all following integers until 4096 is reached, other than the part with the conditional statement that I assume is correct

Ultimately I don't understand how to say that I want it to return each power of 2 up until 4096, I understand what they are but not how to use python to say "I want 2 raised to (2+1) and 2 raised to (2+2) and to raised to (2+3)". Where should I place certain values in the function to create this progression?

I'm very, very new to this and need some help understanding the nomenclature, I'm not asking anyone to due my work for me, just show me the correct way here so that I can derive the correct way later.

4. Oct 23, 2011

### boaz

Code (Text):

stop_pow = 4096
base = 2
# print the seq. 2, 4, 8, ..., 4096
while (base <= stop_pow):
print base
base = base*2

does it work?

Last edited: Oct 23, 2011
5. Oct 23, 2011

### mr.me

Yes it indeed does work. Thank you very much boaz

Seeing that the "base" is multiplied upon each recursion makes sense as how it raises the power of the exponentiation

6. Oct 23, 2011

### boaz

a simple trick that helped me out is taking a paper and like the computer going over each line and writing what is stored in the variables. it sounds odd and like a waste of time, but for a newbie is a life-saver.

7. Oct 23, 2011

### Staff: Mentor

This is excellent advice, and is something that newbies should get in the habit of doing. You don't really understand the code you write unless you know exactly what it is doing.