Python Learning Python Loops: From Counting to Exponents of 2

  • Thread starter Thread starter mr.me
  • Start date Start date
  • Tags Tags
    Loops Python
AI Thread Summary
The discussion revolves around a beginner's attempt to use Python to print powers of 2 up to 4096 using a while loop. The initial code provided was incorrect, as it raised the base to itself and added one, leading to unexpected results. Participants clarified the correct approach, emphasizing that the base should be multiplied by 2 in each iteration to achieve the desired sequence of powers. A corrected version of the code was shared, demonstrating that multiplying the base by 2 in the loop effectively generates the powers of 2. Additionally, a helpful tip was offered: writing down variable values at each step can enhance understanding of how the code operates, which is particularly beneficial for beginners. This method encourages a deeper grasp of programming concepts and syntax.
mr.me
Messages
49
Reaction score
0
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:
Technology news on Phys.org
Please use [noparse]
Code:
[/noparse] tags when posting your code. This is especially important in Python, where

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

is syntactically incorrect, while

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

and

Code:
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.
 
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.
 
what about
Code:
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:
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
 
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.
 
boaz said:
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.
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.
 

Similar threads

Replies
5
Views
2K
Replies
10
Views
2K
Replies
3
Views
1K
Replies
10
Views
1K
Replies
5
Views
2K
Replies
3
Views
1K
Back
Top